ruby on rails - How can I sort records by average rating with latsrate gem? -


i using letsrate gem rating https://github.com/muratguzel/letsrate
how can sort records average rating?
or have write scratch own rating?

it possible sort rating letsrate gem, because of bug, little difficult. take example, app users rate cars on speed, engine, , price.

class car < activerecord::base   attr_accessible :name   letsrate_rateable "speed", "engine", "price" end 

now, can create users, cars, , ratings.

user = user.create!(email: 'user@example.com', password: 'password', password_confirmation: 'password') other = user.create!(email: 'other@example.com', password: 'password', password_confirmation: 'password')  camry = car.create!(name: 'camry') mustang = car.create!(name: 'mustang') ferrari = car.create!(name: 'ferrari')  camry.rate 2, user.id, 'speed' camry.rate 3, user.id, 'engine' camry.rate 5, user.id, 'price' camry.rate 4, user.id mustang.rate 3, user.id, 'speed' mustang.rate 4, user.id, 'engine' mustang.rate 3, user.id, 'price' mustang.rate 3, user.id ferrari.rate 5, user.id, 'speed' ferrari.rate 5, user.id, 'engine' ferrari.rate 1, user.id, 'price' ferrari.rate 5, user.id  camry.rate 3, other.id, 'speed' camry.rate 2, other.id, 'engine' camry.rate 4, other.id, 'price' camry.rate 5, other.id mustang.rate 4, other.id, 'speed' mustang.rate 3, other.id, 'engine' mustang.rate 3, other.id, 'price' mustang.rate 4, other.id ferrari.rate 5, other.id, 'speed' ferrari.rate 4, other.id, 'engine' ferrari.rate 1, other.id, 'price' ferrari.rate 4, other.id 

it easy sort based on overall rating, no dimension, joining rate_average_without_dimension association:

car.joins(:rate_average_without_dimension).order('rating_caches.avg desc') 

you scope out as

scope :sorted_by_rating_without_dimension, joins(:rate_average_without_dimension).order('rating_caches.avg desc') scope :top_ten_without_dimension, sorted_by_rating_without_dimension.limit(10) 

and can include 'top 10' list:

car.top_ten_without_dimension 

however, if want 'top 10 engines' or 'best value' list? should simple as

car.joins(:engine_average).order('rating_caches.avg desc') car.joins(:price_average).order('rating_caches.avg desc') 

but, you'll error

activerecord::configurationerror: association named 'engine_average' not found; perhaps misspelled 

this because letsrate creates associations strings instead of symbols. workaround, can change letsrate_rateable call following:

dimensions = ["speed", "engine", "price"] letsrate_rateable *dimensions  dimensions.each |dimension|   has_one :"#{dimension}_average", :as => :cacheable, :class_name => "ratingcache", :dependent => :destroy, :conditions => {:dimension => dimension.to_s} end 

(note : in front of "#{dimension}_average" interpolation).

now, can use

car.joins(:engine_average).order('rating_caches.avg desc') 

or scopes,

scope :sorted_by_engine_rating, joins(:engine_average).order('rating_caches.avg desc') scope :top_ten_engines, sorted_by_engine_rating.limit(10) 

i have submitted pull request fixes bug. feel free comment or give +1 accepted.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -