Jeff Pritchard
2009-Nov-10 06:48 UTC
should be simple -- will_paginate ordered by acts_as_rated
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? thanks, jp -- Posted via http://www.ruby-forum.com/.
Frederick Cheung
2009-Nov-10 07:11 UTC
Re: should be simple -- will_paginate ordered by acts_as_rated
On Nov 10, 6:48 am, Jeff Pritchard <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> 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. Fred> 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? > > thanks, > jp > -- > Posted viahttp://www.ruby-forum.com/.
Jeff Pritchard
2009-Nov-11 03:07 UTC
Re: should be simple -- will_paginate ordered by acts_as_rat
Frederick Cheung wrote:> On Nov 10, 6:48�am, Jeff Pritchard <rails-mailing-l...@andreas-s.net> > wrote: >> >> 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. > > FredA little help with this? I never did any stand-alone sql and don''t know an inner from an outer join. 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 ? thanks, jp -- Posted via http://www.ruby-forum.com/.
Frederick Cheung
2009-Nov-11 09:14 UTC
Re: should be simple -- will_paginate ordered by acts_as_rat
On Nov 11, 3:07 am, Jeff Pritchard <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> 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. Fred> thanks, > jp > -- > Posted viahttp://www.ruby-forum.com/.
Jeff Pritchard
2009-Nov-11 16:01 UTC
Re: should be simple -- will_paginate ordered by acts_as_rat
Frederick Cheung wrote:> On Nov 11, 3:07�am, Jeff Pritchard <rails-mailing-l...@andreas-s.net> > wrote: >> > 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. > > FredThanks 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 thanks, jp -- Posted via http://www.ruby-forum.com/.
Frederick Cheung
2009-Nov-11 16:29 UTC
Re: should be simple -- will_paginate ordered by acts_as_rat
On Nov 11, 4:01 pm, Jeff Pritchard <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Frederick Cheung wrote: > > And your suggestion (slightly modified by me [single quotes added to > rating_statistics] gives thisit needs to be a symbol (otherwise it assumes you have constructed the join statement already). Fred