Rails db query to find and sort posts

Viewed 169

My app has designs that users can like (vote, using acts_as_voteable). To find a design's like count in the view, you use

@design.votes.count

I'm making a popular page to showcase the most popular designs based on the number of votes they have. I only want designs that has at least 5 votes to them. Right now, I had that in the view but I want to push that into the controller. My controller, thus far, looks like this which shows all the designs and sorts them in order of most votes.

def popular
  @designs = Design.all
  @designs.sort! {|t1, t2| t2.votes.count <=> t1.votes.count}
end

Now i just want to make sure the designs have a minimum vote count of 5.

Previously, I was doing this the wrong way and putting it in my view by putting this inside my Design loop

<% if design.vote.count > 5 %>
  ...
<% end %>

Thanks!

2 Answers
Related