How can I rank records on the index page by multiple values?
I have a table of venues in my app which are displayed on the venues index page in partials with the highest average rated venues at the top and descending:
venues controller
def index
if
@venues = Venue.order("average_rating DESC").all
else
@venues = Venue.all
end
@venues = @venues.paginate :per_page => 15, :page => params[:page]
end
I have just enabled some venues to be free and some to be premium with:
venue.rb
PLANS = %w[free premium]
venue edit.html.erb
<%= f.collection_select :plan, Venue::PLANS, :to_s, :humanize %></p>
How can I make it so all the premium venues are displayed above the free venues even if they have a lower average rating?
Thanks very much for any help its much a开发者_StackOverflow中文版ppreciated!
Something like this should work:
@venues = Venue.order("case plan when 'premium' then 1 else 0 end desc, average_rating DESC").all
精彩评论