开发者

How can I rank records on the index page by multiple values?

开发者 https://www.devze.com 2023-04-07 14:10 出处:网络
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:

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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号