# Should be simple -- will\_paginate ordered by acts\_as\_rated

**URL:** <https://www.ruby-forum.com/t/should-be-simple-will-paginate-ordered-by-acts-as-rated/178727>\
**Category:** Rails\
**Created:** [November 10, 2009, 7:48am UTC](https://www.ruby-forum.com/t/should-be-simple-will-paginate-ordered-by-acts-as-rated/178727 "2009-11-10T07:48:42Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeffpritchard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/43a26b/32.png) [@jeffpritchard](https://www.ruby-forum.com/u/jeffpritchard)\
**Post date:** [November 10, 2009, 7:48am UTC](https://www.ruby-forum.com/t/should-be-simple-will-paginate-ordered-by-acts-as-rated/178727/1 "2009-11-10T07:48:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jeffpritchard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/43a26b/32.png) [@jeffpritchard](https://www.ruby-forum.com/u/jeffpritchard)\
**Post date:** [November 10, 2009, 8:12am UTC](https://www.ruby-forum.com/t/should-be-simple-will-paginate-ordered-by-acts-as-rated/178727/2 "2009-11-10T08:12:47Z")

</div>

On Nov 10, 6:48 am, Jeff P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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.

Fred

---

<div class="post-metadata">

**Author:** ![jeffpritchard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/43a26b/32.png) [@jeffpritchard](https://www.ruby-forum.com/u/jeffpritchard)\
**Post date:** [November 11, 2009, 4:07am UTC](https://www.ruby-forum.com/t/should-be-simple-will-paginate-ordered-by-acts-as-rated/178727/3 "2009-11-11T04:07:40Z")

</div>

Frederick C. wrote:

> On Nov 10, 6:48ï¿½am, Jeff P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> 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.
> 
> Fred

A 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

---

<div class="post-metadata">

**Author:** ![jeffpritchard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/43a26b/32.png) [@jeffpritchard](https://www.ruby-forum.com/u/jeffpritchard)\
**Post date:** [November 11, 2009, 10:17am UTC](https://www.ruby-forum.com/t/should-be-simple-will-paginate-ordered-by-acts-as-rated/178727/4 "2009-11-11T10:17:15Z")

</div>

On Nov 11, 3:07 am, Jeff P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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.

Fred

---

<div class="post-metadata">

**Author:** ![jeffpritchard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/43a26b/32.png) [@jeffpritchard](https://www.ruby-forum.com/u/jeffpritchard)\
**Post date:** [November 11, 2009, 5:31pm UTC](https://www.ruby-forum.com/t/should-be-simple-will-paginate-ordered-by-acts-as-rated/178727/5 "2009-11-11T17:31:03Z")

</div>

On Nov 11, 4:01 pm, Jeff P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Frederick C. wrote:
> 
> And your suggestion (slightly modified by me [single quotes added to  
> rating\_statistics] gives this

it needs to be a symbol (otherwise it assumes you have constructed the  
join statement already).

Fred

---

<div class="post-metadata">

**Author:** ![jeffpritchard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/43a26b/32.png) [@jeffpritchard](https://www.ruby-forum.com/u/jeffpritchard)\
**Post date:** [November 11, 2009, 5:01pm UTC](https://www.ruby-forum.com/t/should-be-simple-will-paginate-ordered-by-acts-as-rated/178727/6 "2009-11-11T17:01:47Z")

</div>

Frederick C. wrote:

> On Nov 11, 3:07ï¿½am, Jeff P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> 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.
> 
> 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

thanks,  
jp
