# Will\_paginate question

**URL:** <https://www.ruby-forum.com/t/will-paginate-question/129586>\
**Category:** Rails\
**Created:** [February 19, 2008, 9:39pm UTC](https://www.ruby-forum.com/t/will-paginate-question/129586 "2008-02-19T21:39:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![pete](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3ab097/32.png) [@pete](https://www.ruby-forum.com/u/pete)\
**Post date:** [February 19, 2008, 9:39pm UTC](https://www.ruby-forum.com/t/will-paginate-question/129586/1 "2008-02-19T21:39:56Z")

</div>

Hi-

Is it possible to use search parameters with will\_paginate?

For example, something like this:

@var = Model.paginate :per\_page =\> 20, :page =\> params[:page],  
:order =\> ‘my\_date DESC’, :conditions  
=\>[“my\_date \>= ? AND my\_date \<=?”,@start,@end]

I am getting some errors with this, but will this generally work?

---

<div class="post-metadata">

**Author:** ![pete](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3ab097/32.png) [@pete](https://www.ruby-forum.com/u/pete)\
**Post date:** [February 20, 2008, 12:53am UTC](https://www.ruby-forum.com/t/will-paginate-question/129586/2 "2008-02-20T00:53:36Z")

</div>

If you don’t get a suitable answer here, you should know that there is  
a will\_paginate Google Group, just like this rubyonrails-talk group.  
Naturally, it’s called “will\_paginate”. 🙂

n

---

<div class="post-metadata">

**Author:** ![pete](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3ab097/32.png) [@pete](https://www.ruby-forum.com/u/pete)\
**Post date:** [February 20, 2008, 2:10pm UTC](https://www.ruby-forum.com/t/will-paginate-question/129586/3 "2008-02-20T14:10:43Z")

</div>

I haven’t used the substitution like you have, but I can do it with a  
conditions hash:

@items = Item.paginate(:all,  
:order =\> “featured DESC, #{@sort.sort}, id DESC”,  
:conditions =\> @filters,  
:page =\> params[:page])

where @filters is a hash like {:hidden =\> false, :category\_id =\> 5}.  
I notice that you don’t have an :all or :first at the beginning.  
Maybe this matters.

-Kyle

---

<div class="post-metadata">

**Author:** ![pete](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3ab097/32.png) [@pete](https://www.ruby-forum.com/u/pete)\
**Post date:** [February 20, 2008, 2:42pm UTC](https://www.ruby-forum.com/t/will-paginate-question/129586/4 "2008-02-20T14:42:37Z")

</div>

Hi Pete,

Yes you can do conditional paginates. See my search method below.

Note that you have to remember the search conditions between pages  
because you have to restate them on calls for subsequent pages. I stick  
mine in the session.

Hope this helps,  
John

def full\_search  
unless params[:page]  
session[:search] = [“category\_id = ? and address\_posttown like ?  
and address\_postcode like ? and published is true”,  
params[:item][:category\_id],  
(params[:item][:address\_posttown].to\_s) +"%",  
(params[:item][:address\_postcode].to\_s) +"%"]  
end

```
@items = Item.paginate :page => params[:page],
                       :conditions => session[:search]

if @items.empty?
  flash.now[:error] = "Sorry, nothing matched your search request"
end

# old-fashioned reload for browsers not doing AJAX
render :action => :search unless request.xhr?

```

end
