ratyrate icon indicating copy to clipboard operation
ratyrate copied to clipboard

how can we sort model items by rating when show model index?

Open oladan opened this issue 10 years ago • 5 comments

can we use order by some field? for example def index @hotels = Hotel.order(????? DESC') end

oladan avatar Nov 21 '14 14:11 oladan

anyone have this issue, people must want to sort_by the rating.

TylerSangster avatar May 20 '15 21:05 TylerSangster

@TylerSangster do you have a solution for this issue?

shazadmaved avatar May 28 '15 08:05 shazadmaved

You want to do an outer join with the rating_caches table so that things that have no rankings will still appear in the list. "Includes" will get you a left outer join.

scope :with_ratings, ->{includes(:rate_average_without_dimension).order("rating_caches.avg desc")}

(rate_average_without_dimension is part of ratyrate and gets added to any model that applies ratyrate_rateable. It is an association definition: has_one :rate_average_without_dimension, -> { where dimension: nil}, :as => :cacheable, :class_name => "RatingCache", :dependent => :destroy)

I have that code inside of a concern that I include in any model code that can be rated. This is using only the overall rankings, not by dimension.

Then you'd just do Hotel.with_ratings and the highest rated hotels would be sorted at the top.

nuclearspike avatar Aug 17 '15 02:08 nuclearspike

@nuclearspike Your code works it sorts movies by rating

panox avatar Nov 02 '15 09:11 panox

Take me off your list

Sent from my iPhone

On Nov 2, 2015, at 4:40 AM, Panagiotis Papantonopoulos [email protected] wrote:

@nuclearspike Your code works it sorts movies by rating

— Reply to this email directly or view it on GitHub.

hotels avatar Nov 02 '15 11:11 hotels