How can I order my record partials by rating?
I have a table开发者_如何学运维 of venues where each has many reviews and each review has a rating (1-5), my venue index page currently shows all the venues as partials in the order they were created with the oldest at the top how can I change this to display the venues with the highest average rating at the top?
My venues controller index currently looks like this:
def index
if
@venues = Venue.with_type(params[:venuetypes]).with_area(params[:areas])
else
@venues = Venue.all
end
end
Thanks for any help!
Using Rails 3? Here's a starting point.
def index
if
@venues = Venue.with_type(params[:venuetypes]).with_area(params[:areas]).joins(:reviews).order("reviews.rating DESC")
else
@venues = Venue.joins(:reviews).order("reviews.rating DESC").all
end
end
精彩评论