steveks17
steveks17

Reputation: 73

Scope usage for index view

I'm trying to use the below scope to order my all reviews page in descending order of the review ratings . How would I implement this? I haved added my index method below.

scope :order_by_rating, ->{left_joins(:reviews).group(:id).order('avg(rating) desc')} 
      def index

        if @tea = Tea.find_by_id(params[:tea_id])
          @reviews = @tea.reviews
        else
          @reviews = Review.all
        end
      end

Upvotes: 2

Views: 264

Answers (1)

Sohail Aslam
Sohail Aslam

Reputation: 727

please change your scope to

scope :order_by_rating, ->{group(:id).order('avg(reviews.rating) desc')}

Upvotes: 3

Related Questions