# MVC and Pagination

**URL:** <https://www.ruby-forum.com/t/mvc-and-pagination/95676>\
**Category:** Rails\
**Created:** [June 8, 2006, 7:55pm UTC](https://www.ruby-forum.com/t/mvc-and-pagination/95676 "2006-06-08T19:55:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dacbo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b9e5f3/32.png) [@dacbo](https://www.ruby-forum.com/u/dacbo)\
**Post date:** [June 8, 2006, 7:55pm UTC](https://www.ruby-forum.com/t/mvc-and-pagination/95676/1 "2006-06-08T19:55:18Z")

</div>

Just wondering how to best integrate with pagination and still stick to  
MVC…

Say I have a Post model that has a published attribute. I want to show  
all the recent published posts on a page.

My first instinct is to create a method in the Post model called recent  
that does the find with proper conditions and order, but the problem I  
have is how do I use that with pagination (which definitely does not  
belong in model)?

Any ideas?

Thanks,

Chris

---

<div class="post-metadata">

**Author:** ![dacbo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b9e5f3/32.png) [@dacbo](https://www.ruby-forum.com/u/dacbo)\
**Post date:** [June 8, 2006, 8:14pm UTC](https://www.ruby-forum.com/t/mvc-and-pagination/95676/2 "2006-06-08T20:14:57Z")

</div>

Try scaffolding and base your own code on that source.  
-Nathan

---

<div class="post-metadata">

**Author:** ![dacbo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b9e5f3/32.png) [@dacbo](https://www.ruby-forum.com/u/dacbo)\
**Post date:** [June 8, 2006, 9:23pm UTC](https://www.ruby-forum.com/t/mvc-and-pagination/95676/3 "2006-06-08T21:23:03Z")

</div>

Umm, scaffolding doesn’t really apply to what I am trying to do below.

Chris

---

<div class="post-metadata">

**Author:** ![dacbo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b9e5f3/32.png) [@dacbo](https://www.ruby-forum.com/u/dacbo)\
**Post date:** [June 8, 2006, 9:51pm UTC](https://www.ruby-forum.com/t/mvc-and-pagination/95676/4 "2006-06-08T21:51:07Z")

</div>

On 8-Jun-06, at 10:52 AM, Chris B. wrote:

> that does the find with proper conditions and order, but the problem I  
> have is how do I use that with pagination (which definitely does not  
> belong in model)?
> 
> Any ideas?

Is there something wrong with doing something like this in your  
controller?

@post\_pages, @posts = paginate(Post,  
:conditions =\> [‘published \> ?’, 1.day.ago],  
:order =\> “published DESC”,  
:parameter =\> ‘page’,  
:per\_page =\> (params[:per\_page] || 10).to\_i  
)

If you’re wanting to fully encapsulate the _idea_ of ‘recent’ in your  
model then go ahead and have a Post.recent method. You’ll also need  
a Post.count\_recent so that in your controller you can set up  
pagination ‘classic’ style (but you’re kind of fighting the  
framework). For example:

in post.rb:

def self.recent(limit = nil, offset = nil)  
self.find(:all,  
:conditions =\> [‘published \> ?’, 1.day.ago],  
:order =\> “published DESC”,  
:limit =\> limit,  
:offset =\> offset  
)  
end

def self.count\_recent  
self.count(conditions =\> [‘published \> ?’, 1.day.ago])  
end

and in your controller:

@post\_pages = Paginator.new(self,  
Post.count\_recent(),  
10, params[:page])  
@post = Post.recent(@post\_pages.items\_per\_page,  
@post\_pages.current.offset)

_WARNING_ - this is off the top of my head, syntax errors might be in  
there 🙂

Either way should work, though just putting it all in the controller  
(first example) is often clearer.

## Regards, Tervor

Trevor S.  
[http://somethinglearned.com](http://somethinglearned.com)
