title: How to calculate and sort by popularity?
body: <p>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.</p>
  
  <p>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?</p>
  
  <p>Also feel free to pick apart anything else you see. I will never learn if I'm never told.</p>
  
  <p>Thank you all for your help in advance,
  Ian</p>
  
  <pre><code>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
  </code></pre>
  
  <p>Cards Table</p>
  
  <pre><code> =&gt; Card(id: integer, user_id: integer, event: text, created_at: datetime, updated_at: datetime, score: integer, comments_count: integer, votes_count: integer)      
  </code></pre>
  
  <p>Votes Table</p>
  
  <pre><code>=&gt; Vote(id: integer, user_id: integer, card_id: integer, up: boolean, created_at: datetime, updated_at: datetime)                                                                                   
  </code></pre>