# Using will\_paginate with arbitrary sql queries

**URL:** <https://www.ruby-forum.com/t/using-will-paginate-with-arbitrary-sql-queries/178588>\
**Category:** Rails\
**Created:** [November 7, 2009, 6:41am UTC](https://www.ruby-forum.com/t/using-will-paginate-with-arbitrary-sql-queries/178588 "2009-11-07T06:41:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![C\_K\_Kashyap](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c37758/32.png) [@C\_K\_Kashyap](https://www.ruby-forum.com/u/C_K_Kashyap)\
**Post date:** [November 7, 2009, 6:41am UTC](https://www.ruby-forum.com/t/using-will-paginate-with-arbitrary-sql-queries/178588/1 "2009-11-07T06:41:25Z")

</div>

Hi All,  
I have a situation where I have a task model, tag model and a link  
table task\_tags.

In a particular view, I need to display a list of task that have a  
certain tags -  
For this, I’d need to run a query and get the results that would need  
to be paginated - so I cannot really call  
paginate on the Task model itself.

1. What is a good way to deal with this situation?
2. In general, how can I use will\_paginate plugin on arbitrary sql  
queries.

–  
Regards,  
Kashyap

---

<div class="post-metadata">

**Author:** ![C\_K\_Kashyap](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c37758/32.png) [@C\_K\_Kashyap](https://www.ruby-forum.com/u/C_K_Kashyap)\
**Post date:** [November 7, 2009, 11:14am UTC](https://www.ruby-forum.com/t/using-will-paginate-with-arbitrary-sql-queries/178588/2 "2009-11-07T11:14:40Z")

</div>

On Nov 7, 5:40 am, C K Kashyap [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 1. What is a good way to deal with this situation?
> 2. In general, how can I use will\_paginate plugin on arbitrary sql queries.

There’s sort of an example of this in the docs:

@entries = WillPaginate::Collection.create(1, 10) do |pager|  
result = Post.find(:all, :limit =\> pager.per\_page, :offset =\>  
pager.offset)

# inject the result array into the paginated collection:

pager.replace(result)

unless pager.total\_entries  
# the pager didn’t manage to guess the total count, do it manually  
pager.total\_entries = Post.count  
end  
end

replace Post.find :all with a method that performs your query, using  
the limit and offset passed to it and you should be fine. You may also  
need to set pager.total\_entries

Fred

---

<div class="post-metadata">

**Author:** ![C\_K\_Kashyap](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c37758/32.png) [@C\_K\_Kashyap](https://www.ruby-forum.com/u/C_K_Kashyap)\
**Post date:** [November 7, 2009, 7:14pm UTC](https://www.ruby-forum.com/t/using-will-paginate-with-arbitrary-sql-queries/178588/3 "2009-11-07T19:14:00Z")

</div>

You also could use paginate\_by\_sql

2009/11/7, C K Kashyap [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> 1. What is a good way to deal with this situation?
> 2. In general, how can I use will\_paginate plugin on arbitrary sql queries.
> 
> –  
> Regards,  
> Kashyap
> 
> >

–  
Von meinen Mobilgerät aus gesendet
