How to calculate and sort by popularity?

878 Views Asked by At

I'm working on adding a most popular to my display options. I'm not sure if this equation is the one I want to use but I figure the math is the irreverent part of my question, but free for suggested improvements.

My issue is with "@cards.pop = (@W + @R) + ((1 - @W) * @R0)" I'm getting a "undefined method `pop=' for nil:NilClass" error code. Can I not or how can I add a value to the string array, and then use it as a sort option such as the other "view" options have?

Also feel free to pick apart anything else you see. I will never learn if I'm never told.

Thank you all for your help in advance, Ian

if params[:view].present?
  if params[:view] == 'new' #Sort newest to oldest
    @cards = Card.order('created_at DESC').page(params[:page])
  elsif params[:view] == 'old' #Sort oldest to newest
    @cards = Card.order('created_at ASC').page(params[:page])
  elsif params[:view] == 'talk' #Sort most talked about
    @cards = Card.order('comments_count DESC').page(params[:page])
  elsif params[:view] == 'pop' #Sort most popular
    # http://math.stackexchange.com/questions/41459/how-can-i-calculate-most-popular-more-accurately
    @cards = Card.all
    @R0 = Card.average("score")
    @nMax = Card.maximum("votes_count")
    @cards.each do |card|
      @R = card.score
      @n = card.votes_count
      @W = @n / @nMax
      @cards.pop = (@W + @R) + ((1 - @W) * @R0)
    end
    # Need to add a Cards.order line - Not sure how to go about this.
  else
    @cards = Card.order('score DESC').page(params[:page])
  end
else
  @cards = Card.order('score DESC').page(params[:page])
end

Cards Table

 => Card(id: integer, user_id: integer, event: text, created_at: datetime, updated_at: datetime, score: integer, comments_count: integer, votes_count: integer)      

Votes Table

=> Vote(id: integer, user_id: integer, card_id: integer, up: boolean, created_at: datetime, updated_at: datetime)                                                                                   
1

There are 1 best solutions below

0
On

You can move a part of your ( compute popularity ) code into a model method :

class Article < ActiveRecord::Base
...
  def popularity
    @R0 = Card.average("score")
    @nMax = Card.maximum("votes_count")
    @R = score
    @n = votes_count
    @W = @n / @nMax
    (@W + @R) + ((1 - @W) * @R0)
  end
...
end

and after that sort you collection like that :

elsif params[:view] == 'pop' #Sort most popular
  @cards = Card.all.sort{|a, b| a.popularity <=> b.popularity}
end