It seems like this should be very simple and very common.
I'm using "acts_as_rated" plugin to rate images
I'm using mislav-will_paginate gem to paginate a list of images
(not really relevant, but also using paperclip to attach the photo to
the "image" model
I figured I could do something like this:
@images = Image.paginate(:page => params[:page], :per_page => 10, :order
=> 'rating_avg')
rating_avg is the name of the average rating in acts as rated
I get this error:
Mysql::Error: Unknown column 'rating_avg' in 'order clause': SELECT *
FROM `images` ORDER BY rating_avg LIMIT 0, 10
I'm using the:
:with_stats_table => true
option
What's the right way to do this rather obvious action of sorting the
output of a rated model and paginating with will_paginate?
It seems like this should be very simple and very common.
I'm using "acts_as_rated" plugin to rate images
I'm using mislav-will_paginate gem to paginate a list of images
(not really relevant, but also using paperclip to attach the photo to
the "image" model
I figured I could do something like this:
@images = Image.paginate(:page => params[:page], :per_page => 10, :order
=> 'rating_avg')
rating_avg is the name of the average rating in acts as rated
I'm not familiar with acts_as_rated, however: where is the rating_avg
column ? if it is on some separate table then you'll need to join on
that table.
If the class that we apply acts_as_rated to ("images" in my case) winds
up with a
has_one :rating_statistic
and the rating_statistics model
belongs_to :image
and "rating_avg" is a member of the statistics table...
What would the join look like to :order by :rating_avg ?
Well unless you care particularly about what the join looks like you
can just add :joins => rating_statistic to your find. Do take the time
to familiarize yourself with this sort of stuff though or it will bite
you on the ass eventually.
Well unless you care particularly about what the join looks like you
can just add :joins => rating_statistic to your find. Do take the time
to familiarize yourself with this sort of stuff though or it will bite
you on the ass eventually.
Fred
Thanks Fred. No joy.
This works fine but doesn't order anything (obviously):
@images = Image.paginate(:page => params[:page], :per_page => 10)
This
@images = Image.paginate(:page => params[:page], :per_page => 10, :order
=> 'rating_avg')
gives me this error:
Mysql::Error: Unknown column 'rating_avg' in 'order clause': SELECT *
FROM `images` ORDER BY rating_avg LIMIT 0, 10
And your suggestion (slightly modified by me [single quotes added to
rating_statistics] gives this
@images = Image.paginate(:page => params[:page], :per_page => 10, :joins
=> 'rating_statistics', :order => 'rating_avg')
Mysql::Error: Unknown table 'images': SELECT `images`.* FROM `images`
rating_statistics ORDER BY rating_avg LIMIT 0, 10