In my app, I have a Product model which has_many Reviews.
In my controller, I'd just like to order the array of Products by the count of how many reviews there are.
Controller
@search = Product.search_attributes(params[:search])
@products = @search.sort_by_reviews_count
Product model:
def self.sort_by_reviews_count
self.sort! { |x,y| x.reviews.count <=> y.reviews.count }
end
However, I am getting the following error:
undefined method `sort!' for #<Class:0x007ff019ebf468>
Why is this happening?