# Pagination has many through problems

**URL:** <https://www.ruby-forum.com/t/pagination-has-many-through-problems/103800>\
**Category:** Rails\
**Created:** [May 10, 2007, 1:22pm UTC](https://www.ruby-forum.com/t/pagination-has-many-through-problems/103800 "2007-05-10T13:22:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ksader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/49beb7/32.png) [@ksader](https://www.ruby-forum.com/u/ksader)\
**Post date:** [May 10, 2007, 1:22pm UTC](https://www.ruby-forum.com/t/pagination-has-many-through-problems/103800/1 "2007-05-10T13:22:09Z")

</div>

I have searched for days for an example that demonstrates what i would  
like to do, and this morning i thought i’d worked it out, but  
no…here’s what i have:

# Controller

class ProfilesController \< ApplicationController  
def friends  
current\_user.profile.friends  
end  
def list\_friends  
@profile\_pages, @profiles = paginate( friends, :per\_page =\> 10)  
end  
end

# Models

class Profile \< ActiveRecord::Base  
has\_many :friendships  
has\_many :friends,  
:through =\> :friendships  
end

class Friendship \< ActiveRecord::Base  
belongs\_to :profile  
belongs\_to :friend, :class\_name =\> “Profile”, :foreign\_key =\>  
“friend\_id”  
end

When accessing “[http://localhost:3000/profiles/list\_friends](http://localhost:3000/profiles/list_friends)” i get the  
error:

“#Profile:0x10ee01b4#Profile:0x10ee018c” is not a valid constant  
name!

Any help gratefully recieved!!

keith

---

<div class="post-metadata">

**Author:** ![ksader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/49beb7/32.png) [@ksader](https://www.ruby-forum.com/u/ksader)\
**Post date:** [May 10, 2007, 1:28pm UTC](https://www.ruby-forum.com/t/pagination-has-many-through-problems/103800/2 "2007-05-10T13:28:59Z")

</div>

Hey,

The first option to paginate expects the Symbol object which is the  
name of your table, not an ActiveRecord or a list of ActiveRecord  
objects.

Cheers,  
Yuri

On 5/10/07, Keith S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> current\_user.profile.friends  
> class Profile \< ActiveRecord::Base
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

–  
Best regards,  
Yuri L.

---

<div class="post-metadata">

**Author:** ![ksader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/49beb7/32.png) [@ksader](https://www.ruby-forum.com/u/ksader)\
**Post date:** [May 10, 2007, 2:20pm UTC](https://www.ruby-forum.com/t/pagination-has-many-through-problems/103800/3 "2007-05-10T14:20:46Z")

</div>

Yuri L. wrote:

> The first option to paginate expects the Symbol object which is the  
> name of your table, not an ActiveRecord or a list of ActiveRecord  
> objects.

Hey Yuri,

Thanks for replying so quickly!!

So does this mean i am unable to paginate a recordset that is the result  
of two tables joined?

From the models you can see the only way for me to derive a list of  
“friends” is from the “friendships” table.

So do i have to paginate that, and then request the “profile” for each  
friendship?

thanks  
keith

---

<div class="post-metadata">

**Author:** ![ksader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/49beb7/32.png) [@ksader](https://www.ruby-forum.com/u/ksader)\
**Post date:** [May 10, 2007, 4:20pm UTC](https://www.ruby-forum.com/t/pagination-has-many-through-problems/103800/4 "2007-05-10T16:20:15Z")

</div>

Many thanks Yuri, i did actually have that in my application controller  
from previous attempts, but you’re right it does do exactly what i’m  
looking for.

For anyone looking for the solution, after including Yuri’s suggested  
method in my application controller, my profiles controller eventually  
looks like this:

class ProfilesController \< ApplicationController  
def list\_friends  
@friends = current\_user.profile.friends  
@profile\_pages, @profiles = paginate\_collection(@friends, :page =\>  
params[:page])  
end  
end

This does work as expected, however the scalability issue does concern  
me, and i’ve been trying to absorb and understand as much as possible  
about this problem.

Yuri:

Do you have any specific advice with regards to this issue?

I had read through this post  
([http://www.igvita.com/blog/2006/09/10/faster-pagination-in-rails/](http://www.igvita.com/blog/2006/09/10/faster-pagination-in-rails/))  
which suggests the use of paginating\_find plugin, but i dont see how  
this could be used with more than one model.

thanks  
keith

---

<div class="post-metadata">

**Author:** ![ksader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/49beb7/32.png) [@ksader](https://www.ruby-forum.com/u/ksader)\
**Post date:** [May 10, 2007, 6:48pm UTC](https://www.ruby-forum.com/t/pagination-has-many-through-problems/103800/5 "2007-05-10T18:48:02Z")

</div>

Hi Keith. I know we emailed on this already, but I thought I’d post  
this for others. In your situation, paginating\_find works like this:

@friends = current\_user.profile.friends.find(:all, :page =\> { :size =\>  
10, :current =\> params[:page] })

… which, of course, gets you 10 friends associated with the current  
user (through the profile). Which 10 friends depends on the value of  
params[:page]. So, no more loading _all_ the friends if you only want  
to show 10 of them at a time…

On May 10, 9:20 am, Keith S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![ksader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/49beb7/32.png) [@ksader](https://www.ruby-forum.com/u/ksader)\
**Post date:** [May 10, 2007, 2:29pm UTC](https://www.ruby-forum.com/t/pagination-has-many-through-problems/103800/6 "2007-05-10T14:29:09Z")

</div>

One of the options is having the following method in your  
ApplicationController

def paginate\_collection(collection, options = {})  
default\_options = {:per\_page =\> 10, :page =\> params[:page]}  
options = default\_options.merge(options)

```
pages = Paginator.new(self, collection.size, options[:per_page],

```

options[:page])  
first = pages.current.offset  
last = [first + options[:per\_page], collection.size].min  
slice = collection[first…last]  
return [pages, slice]  
end

This method receives the collection as you see, and returns exactly  
what paginate returns. The disadvantage of this solution is that you  
have to read the _whole_ list of objects into memory. Though, well,  
your initial variant does the same 🙂

Cheers.

On 5/10/07, Keith S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> So does this mean i am unable to paginate a recordset that is the result
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

–  
Best regards,  
Yuri L.
