# Strategy for persisting row filter variables in pagination?

**URL:** <https://www.ruby-forum.com/t/strategy-for-persisting-row-filter-variables-in-pagination/101220>\
**Category:** Rails\
**Created:** [August 3, 2006, 5:03pm UTC](https://www.ruby-forum.com/t/strategy-for-persisting-row-filter-variables-in-pagination/101220 "2006-08-03T17:03:05Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/eada6e/32.png) [@dan](https://www.ruby-forum.com/u/dan)\
**Post date:** [August 3, 2006, 5:03pm UTC](https://www.ruby-forum.com/t/strategy-for-persisting-row-filter-variables-in-pagination/101220/1 "2006-08-03T17:03:05Z")

</div>

I have my list view filtering rows shown within the table

 tags like  
most of the introductory tutorials do. As I’m adding a few dozen entries  
I’m noticing the standard scaffolding pagination doesn’t pick up the  
number of rows from the filter since it is not being passed the number  
of rows returned within the scope of the filter statement.

From a tactical point I’m wondering if there’s a proper way to approach  
this. Should I move my row filter statements into a a method within a  
helper that can then be called at top of the list view for the entire  
page? Or perhaps separate partial view files should be used with  
different filter statements at top of each? Or perhaps there’s someother  
way I’m not seeing right off.

I’m imagining the pagination link\_to’s will need to include the filter  
variables so they are persistent also. So ":s =\> “Summer”, :c =\>  
“Beverage”, :t =\> “Cold” creates urls with added  
“?s=Summer&c=Beverages&t=Cold” for example. Is there a prettier way to  
persist the variables? Maybe iterate through possible #? of params  
within the method?

Thanks,  
DAN

---

<div class="post-metadata">

**Author:** ![dan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/eada6e/32.png) [@dan](https://www.ruby-forum.com/u/dan)\
**Post date:** [August 3, 2006, 7:59pm UTC](https://www.ruby-forum.com/t/strategy-for-persisting-row-filter-variables-in-pagination/101220/2 "2006-08-03T19:59:27Z")

</div>

I haven’t got it working yet, but maybe this is the right direction?

In my snacks\_controller.rb I’m playing with using:

def list  
@s = @params[‘s’]  
@c = @params[‘c’]  
@t = @params[‘t’]  
@snacks = Snack.find(:all, @s == @snacks.season, @c ==  
@snacks.category, @t == @snacks.temp)  
@snack\_pages, @snacks = paginate :snacks, :per\_page =\> 5  
end

This way paginate should be getting the rows from the @snacks find  
query, right?

I’m getting “undefined method `season’ for #Array:0x24e6b84”  
currently. It doesn’t seem to recognize the current rows season\_id table  
column for some reason, even if I explicitly use @snacks.season\_id.

Any suggestions appreciated.

---

<div class="post-metadata">

**Author:** ![dan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/eada6e/32.png) [@dan](https://www.ruby-forum.com/u/dan)\
**Post date:** [August 4, 2006, 1:37am UTC](https://www.ruby-forum.com/t/strategy-for-persisting-row-filter-variables-in-pagination/101220/3 "2006-08-04T01:37:20Z")

</div>

Well I’ve been working at this for about five hours now and I can’t seem  
to get beyond the basics in RoR. I’ve read and reread through a couple  
more tutorials, dug through my Pick Axe and Ruby for Rails books, and  
tried a few more experiments but still not making any progress. It’s  
great seeing how a basic application can be created in 15 minutes in the  
tutorials but creating a more evolved app seems to be a much more  
complicated process.

To try and further my progress I’ve posted a live working example with  
code and notes so people can see what I am describing. It’s currently  
posted at:  
[Freelance Web Designer](http://www.freelancewebdesigner.com/rails/menu/public/snacks).

This is a clean rewrite starting over from the beginning so hopefully  
it’s straight forward. None

Hopefully someone knows how I can get the pagination to work properly  
while showing only certain rows.

Thanks in advance for any enlightenment.

---

<div class="post-metadata">

**Author:** ![dan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/eada6e/32.png) [@dan](https://www.ruby-forum.com/u/dan)\
**Post date:** [August 4, 2006, 5:28am UTC](https://www.ruby-forum.com/t/strategy-for-persisting-row-filter-variables-in-pagination/101220/4 "2006-08-04T05:28:43Z")

</div>

On 8/3/06, DAN [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> different filter statements at top of each? Or perhaps there’s someother  
> way I’m not seeing right off.
> 
> I’m imagining the pagination link\_to’s will need to include the filter  
> variables so they are persistent also. So ":s =\> “Summer”, :c =\>  
> “Beverage”, :t =\> “Cold” creates urls with added  
> “?s=Summer&c=Beverages&t=Cold” for example. Is there a prettier way to  
> persist the variables? Maybe iterate through possible #? of params  
> within the method?

I store my filter conditions into a session variable, since it gets  
really ugly when you’ve got more than a half a dozen filtering  
conditions.

Mike

---

<div class="post-metadata">

**Author:** ![dan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/eada6e/32.png) [@dan](https://www.ruby-forum.com/u/dan)\
**Post date:** [August 4, 2006, 5:22am UTC](https://www.ruby-forum.com/t/strategy-for-persisting-row-filter-variables-in-pagination/101220/5 "2006-08-04T05:22:41Z")

</div>

On Thursday, August 03, 2006, at 7:59 PM, DAN wrote:

> @snack\_pages, @snacks = paginate :snacks, :per\_page =\> 5
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

try this…

==== controller ====  
def list  
@s = params[‘s’]  
@c = params[‘c’]  
@t = params[‘t’] #use params[:a] instead of @params[:a]  
@snacks = Snack.find(:all, :conditions=\>[“season = ? and category = ?  
and temp = ?”,s,c,t])

# returns an Array of snacks

@snack\_pages, @snacks = paginate :snacks, :per\_page =\> 5  
end

==== view ====

\<% for snack in @snacks %\>  
\<%= snack.season %\>  
\<% end %\>

\_Kevin  
[www.sciwerks.com](http://www.sciwerks.com)

---

<div class="post-metadata">

**Author:** ![dan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/eada6e/32.png) [@dan](https://www.ruby-forum.com/u/dan)\
**Post date:** [August 4, 2006, 11:13pm UTC](https://www.ruby-forum.com/t/strategy-for-persisting-row-filter-variables-in-pagination/101220/6 "2006-08-04T23:13:32Z")

</div>

I’m not sure how that would work either, as the call to paginate will  
overwrite the @snacks created  
in the previous line.

How about this?

@snack\_pages = Paginator.new self, Snack.count, 10, @params[‘page’]  
@snacks = Snack.find :all,  
:conditions=\>[“season = ? and category = ? and temp = ?”,s,c,t])  
:limit =\> @snack\_pages.items\_per\_page,  
:offset =\> @snack\_pages.current.offset
