Search code examples
ruby-on-railsrating

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!


Solution

  • 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