ratyrate
ratyrate copied to clipboard
how can we sort model items by rating when show model index?
can we use order by some field? for example def index @hotels = Hotel.order(????? DESC') end
anyone have this issue, people must want to sort_by the rating.
@TylerSangster do you have a solution for this issue?
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 Your code works it sorts movies by rating
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.