# Good sample for Pagination with Many to Many Relationship?

**URL:** <https://www.ruby-forum.com/t/good-sample-for-pagination-with-many-to-many-relationship/67608>\
**Category:** Rails\
**Created:** [September 5, 2006, 4:59pm UTC](https://www.ruby-forum.com/t/good-sample-for-pagination-with-many-to-many-relationship/67608 "2006-09-05T16:59:35Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![homerlex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/bcef8e/32.png) [@homerlex](https://www.ruby-forum.com/u/homerlex)\
**Post date:** [September 5, 2006, 4:59pm UTC](https://www.ruby-forum.com/t/good-sample-for-pagination-with-many-to-many-relationship/67608/1 "2006-09-05T16:59:35Z")

</div>

Let’s say we have 2 tables with Many to many relationship?

Actors and Movies. Actors can be in many movies and movies have many  
actors.

I want a query that does a text search on the Movie name and finds all  
movies with the word “Shark” in it. The result page would be a list  
sorted by Movie Title/Actor name like:

Movie Actor

* * *

Sharks Rule Barnes, Oscar  
Sharks Rule Cole, Tony  
Sharks Rule Kentz, Olivia  
Teasing Sharks Hardy, Fool  
Teasing Sharks Trouble, Wanting

I only want 25 Movie/Actor lines per page. Any good samples for getting  
this done?

Regards

---

<div class="post-metadata">

**Author:** ![homerlex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/bcef8e/32.png) [@homerlex](https://www.ruby-forum.com/u/homerlex)\
**Post date:** [September 6, 2006, 12:24am UTC](https://www.ruby-forum.com/t/good-sample-for-pagination-with-many-to-many-relationship/67608/2 "2006-09-06T00:24:44Z")

</div>

Hi,

Something like this might work…

def search  
session[:pag\_cache] ||= your\_search\_method  
#with the return result formatted to work with “:conditions=\>”  
@movie\_pages, @movies= paginate :movies, :order\_by =\> ‘title ASC’,  
:per\_page =\> 25, :conditions=\> session[:pag\_cache]  
end
