# Moving pagination to the Model

**URL:** <https://www.ruby-forum.com/t/moving-pagination-to-the-model/130599>\
**Category:** Rails\
**Created:** [February 29, 2008, 6:12pm UTC](https://www.ruby-forum.com/t/moving-pagination-to-the-model/130599 "2008-02-29T18:12:33Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![neil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/839c29/32.png) [@neil](https://www.ruby-forum.com/u/neil)\
**Post date:** [February 29, 2008, 6:12pm UTC](https://www.ruby-forum.com/t/moving-pagination-to-the-model/130599/1 "2008-02-29T18:12:33Z")

</div>

I’m using will\_paginate in a Post class method;

`def self.list(page)
  paginate :per_page => 10,
           :page => page,
           :include => :user
end`

I can happily use the method on my posts controller index action;

```auto
class PostsController < ApplicationController

def index
  @posts = Post.list(params[:page])

   respond_to do |format|
     format.html # index.html.erb
     format.js
   end
 end
```

But I haven’t worked out how to do it on my Users controller show  
action;

```auto
class UsersController < ApplicationController

  def show
    @user = User.find(params[:id])
    @posts = @user.posts.paginate :per_page => 10,
                                  :page => params[:page]
  end
```

How I do I go about using the post.list class method on the post  
object within the @posts variable (therefore DRYing up the pagination  
and leaving it in the model)?

---

<div class="post-metadata">

**Author:** ![neil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/839c29/32.png) [@neil](https://www.ruby-forum.com/u/neil)\
**Post date:** [February 29, 2008, 11:56pm UTC](https://www.ruby-forum.com/t/moving-pagination-to-the-model/130599/2 "2008-02-29T23:56:01Z")

</div>

Neil wrote:

> But I haven’t worked out how to do it on my Users controller show  
> action;
> 
> ```auto
> class UsersController < ApplicationController
> 
> def show
> @user = User.find(params[:id])
> @posts = @user.posts.paginate :per_page => 10,
> :page => params[:page]
> end
> ```
> 
> How I do I go about using the post.list class method on the post  
> object within the @posts variable (therefore DRYing up the pagination  
> and leaving it in the model)?

The 1 to many association between posts can users that AR builds for you  
(as an array) can be extended with :extend

check out ActiveRecord::Base::has\_many() :extend

module PaginationExtension  
def paginate  
#your code here  
end  
end

class User \< ActiveRecord::Base  
has\_many :posts, :extend PaginationExtension  
end

@some\_user.posts.paginate

hth

ilan

---

<div class="post-metadata">

**Author:** ![neil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/839c29/32.png) [@neil](https://www.ruby-forum.com/u/neil)\
**Post date:** [March 2, 2008, 8:48am UTC](https://www.ruby-forum.com/t/moving-pagination-to-the-model/130599/3 "2008-03-02T08:48:55Z")

</div>

Thanks ilan. Just checked out the AP on associated extensions, but I  
have one question; where should I keep the PaginationExtension module?  
Posts are the only thing being paginated, so I presume I’d put the  
extension at the top of the post.rb. Is this correct? Or should I create  
a pagination\_extension.rb?

---

<div class="post-metadata">

**Author:** ![neil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/839c29/32.png) [@neil](https://www.ruby-forum.com/u/neil)\
**Post date:** [March 2, 2008, 8:50am UTC](https://www.ruby-forum.com/t/moving-pagination-to-the-model/130599/4 "2008-03-02T08:50:16Z")

</div>

Neil C. wrote:

> Thanks ilan. Just checked out the AP on associated extensions, but I  
> have one question; where should I keep the PaginationExtension module?  
> Posts are the only thing being paginated, so I presume I’d put the  
> extension at the top of the post.rb. Is this correct? Or should I create  
> a pagination\_extension.rb?

Should read ‘Just checked out the API on associated extensions’.

---

<div class="post-metadata">

**Author:** ![neil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/839c29/32.png) [@neil](https://www.ruby-forum.com/u/neil)\
**Post date:** [March 2, 2008, 5:08pm UTC](https://www.ruby-forum.com/t/moving-pagination-to-the-model/130599/5 "2008-03-02T17:08:59Z")

</div>

Neil C. wrote:

> Thanks ilan. Just checked out the AP on associated extensions, but I  
> have one question; where should I keep the PaginationExtension module?  
> Posts are the only thing being paginated, so I presume I’d put the  
> extension at the top of the post.rb. Is this correct? Or should I create  
> a pagination\_extension.rb?

If that is the only model using it, then it’s fine to keep it in that  
model file.. If on the other hand, it is to be used by more than one  
model then it should be in it’s own file withing app/model or lib/

I like to embed my extension modules within the model itself as:  
class Foo \< ActiveRecord::Base  
module SomeExtension  
#  
end

has\_many :somethings, :extend =\> SomeExtension  
end

hth

ilan

---

<div class="post-metadata">

**Author:** ![neil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/839c29/32.png) [@neil](https://www.ruby-forum.com/u/neil)\
**Post date:** [March 3, 2008, 3:53am UTC](https://www.ruby-forum.com/t/moving-pagination-to-the-model/130599/6 "2008-03-03T03:53:00Z")

</div>

Neil C. wrote:

> module PaginationExtension  
> def pagination(page)  
> paginate :per\_page =\> 10,  
> :page =\> page  
> end  
> end
> 
> But I currently need to keep a class method on the Post.rb to keep it  
> paginating on itself;
> 
> class Post \< ActiveRecord::Base
> 
> def self.pagination(page)  
> paginate :per\_page =\> 10,  
> :page =\> page,  
> :include =\> :user  
> end
> 
> end
> 
> Any way round this?

class Post \< ActiveRecord::Base

module PaginationExtension  
def pagination(page, options ={})  
paginate {:per\_page =\> 10,:page =\> page}.merge options  
end  
end

has\_many :whatevers, :extend =\> PaginationExtension

extend PaginationExtension # no reason we can’t extend it ourselves,  
def pagination(page, options = {})  
super(page, {:include =\> :user}.merge options  
end

end

NOT TESTED,NOT TESTED.. use at your own risk..  
I believe this may be a little drier and meets your criteria..

hth

ilan

---

<div class="post-metadata">

**Author:** ![neil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/839c29/32.png) [@neil](https://www.ruby-forum.com/u/neil)\
**Post date:** [March 2, 2008, 8:31pm UTC](https://www.ruby-forum.com/t/moving-pagination-to-the-model/130599/7 "2008-03-02T20:31:16Z")

</div>

It’s all working, thanks.

Sorry if I’m missing something basic, but can I somehow reuse the  
PaginationExtension on the Post.rb itself (the model being extended  
through the has\_many)? I was hoping to keep the pagination settings in  
just one place.

The pagination\_extension.rb resides in the model folder;

module PaginationExtension  
def pagination(page)  
paginate :per\_page =\> 10,  
:page =\> page  
end  
end

But I currently need to keep a class method on the Post.rb to keep it  
paginating on itself;

class Post \< ActiveRecord::Base

def self.pagination(page)  
paginate :per\_page =\> 10,  
:page =\> page,  
:include =\> :user  
end

end

Any way round this?

---

<div class="post-metadata">

**Author:** ![neil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/839c29/32.png) [@neil](https://www.ruby-forum.com/u/neil)\
**Post date:** [March 3, 2008, 9:16am UTC](https://www.ruby-forum.com/t/moving-pagination-to-the-model/130599/8 "2008-03-03T09:16:47Z")

</div>

Ilan B. wrote:

> Neil C. wrote:
> 
> > module PaginationExtension  
> > def pagination(page)  
> > paginate :per\_page =\> 10,  
> > :page =\> page  
> > end  
> > end
> > 
> > But I currently need to keep a class method on the Post.rb to keep it  
> > paginating on itself;
> > 
> > class Post \< ActiveRecord::Base
> > 
> > def self.pagination(page)  
> > paginate :per\_page =\> 10,  
> > :page =\> page,  
> > :include =\> :user  
> > end
> > 
> > end
> > 
> > Any way round this?
> 
> class Post \< ActiveRecord::Base
> 
> module PaginationExtension  
> def pagination(page, options ={})  
> paginate {:per\_page =\> 10,:page =\> page}.merge options  
> end  
> end
> 
> has\_many :whatevers, :extend =\> PaginationExtension
> 
> extend PaginationExtension # no reason we can’t extend it ourselves,  
> def pagination(page, options = {})  
> super(page, {:include =\> :user}.merge options  
> end
> 
> end
> 
> NOT TESTED,NOT TESTED.. use at your own risk..  
> I believe this may be a little drier and meets your criteria..
> 
> hth
> 
> ilan

My ‘has\_many :posts, :extend =\> PaginationExtension’ lives in the  
User.rb, but the post model is the one being extended with pagination.

I tried putting ‘module PaginationExtension’ and the ‘extend  
PaginationExtension’ in the Post.rb whilst keeping the ‘has\_many :posts,  
:extend =\> PaginationExtension’ in the User.rb, but I get a syntax  
error;

app/models/post.rb:5: syntax error, unexpected tASSOC, expecting ‘}’  
paginate {:per\_page =\> 10, :page =\> page}.merge options
