# Trying to create search filter persistence using SearchLogic

**URL:** <https://www.ruby-forum.com/t/trying-to-create-search-filter-persistence-using-searchlogic/185023>\
**Category:** Rails\
**Created:** [March 11, 2010, 11:11pm UTC](https://www.ruby-forum.com/t/trying-to-create-search-filter-persistence-using-searchlogic/185023 "2010-03-11T23:11:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dburges](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7bcc69/32.png) [@dburges](https://www.ruby-forum.com/u/dburges)\
**Post date:** [March 11, 2010, 11:11pm UTC](https://www.ruby-forum.com/t/trying-to-create-search-filter-persistence-using-searchlogic/185023/1 "2010-03-11T23:11:56Z")

</div>

I have just set up a search form on my index using SearchLogic- it  
works great. However, since there are several search parameters in the  
form I want the results of the most recent search to remain in effect  
until the user does a new search. I can’t figure out how to do this!  
I’ve tried a few things with global and session variables but just  
can’t get it working.

Any advice would be greatly appreciated!

---

<div class="post-metadata">

**Author:** ![dburges](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7bcc69/32.png) [@dburges](https://www.ruby-forum.com/u/dburges)\
**Post date:** [March 16, 2010, 9:22am UTC](https://www.ruby-forum.com/t/trying-to-create-search-filter-persistence-using-searchlogic/185023/2 "2010-03-16T09:22:00Z")

</div>

I have done something along these lines using a database store. I  
dont use SearchLogic, but build my searches using anonymous scope and  
also use Thinking Sphinx.

I have a search model with attributes as below:

Where the search was made from:  
t.string “user”  
t.string “model\_name”  
t.string “controller”  
Details of the search  
t.text “search\_filter”  
t.string “search\_type”  
t.string “search\_field”  
t.integer “page”  
t.integer “per\_page”  
t.string “name”

user holds the id for the user making this search.  
search\_filter contains the search fields serialized, so it can save  
fields for searches against any model. I also save the controller  
since I may be searching the same model from different controllers and  
want to keep separate filters.  
Search field contains the search term if it is a Sphinx search, and  
search\_type defines if the search is normal or sphinx  
I have a name field which I have not yet implemented but which is  
intended eventually to allow users to store pre defined named  
searches.  
The user is also allowed to change the per\_page setting so I save that  
too.

To use the search in the controller requires three standard method  
calls as below:  
@search=Search.set\_search(@user,Product,params)  
@filter=@search.filter  
@products=@search.do\_search

set\_search does an update or create for this user,model,controller.  
It uses the params hash to get the controller and to serialize the  
current filter fields.

The do search method in the search model makes a call to a method  
which I have called ‘filtered’ in each model being searched (so the  
actual search is local to each model). I havnt used search\_logic, but  
I guess with that you wouldnt need to have a specific method in each  
model. In the ‘filtered’ method in the model being searched, I check  
to see if I am doing a normal database search, or if it is Sphinx  
search (my form has a search box as well as filter fields).

For the filter fields, I use an instance of the model being searched  
which makes building the search form really easy.

It may not be the best approach, but it works for me and I have done  
my best to minimise and standardise the code in the controller.  
I hope it may at least give you some ideas or provoke some other  
responses.

Tonypm

---

<div class="post-metadata">

**Author:** ![dburges](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7bcc69/32.png) [@dburges](https://www.ruby-forum.com/u/dburges)\
**Post date:** [April 2, 2010, 11:05pm UTC](https://www.ruby-forum.com/t/trying-to-create-search-filter-persistence-using-searchlogic/185023/3 "2010-04-02T23:05:20Z")

</div>

dburges wrote:

> I have just set up a search form on my index using SearchLogic- it  
> works great. However, since there are several search parameters in the  
> form I want the results of the most recent search to remain in effect  
> until the user does a new search. I can’t figure out how to do this!  
> I’ve tried a few things with global and session variables but just  
> can’t get it working.
> 
> Any advice would be greatly appreciated!

The previous form values you want for re-initializing the form are  
passed into your controller method in the params object. If you have  
built your form around an object called @search like this:

form\_for @search do |f|

you just need to initialize @search with the search values in params, in  
the controller method (perhaps Controller#index), like this:

@search = Model.search(params[:search])

If you have fields that are not part of Model that you want to include  
in the search and have those initialized to, define a named\_scope in  
Model, and use that named\_scope in the form for @search. Then it will  
also be part of the params search values and get initialized along with  
the other, automatically generated named scopes that correspond to the  
model fields.

For example, to add a check box that limits the search to rows with a  
certain condition, you could define a named scope in Model.rb like this:

named\_scope :my\_condition, :conditions =\> “your sql condition here”

Then in the view form, you would include:  
\<%= f.label :my\_condition, “Check to apply my condition” %\>  
\<%= f.check\_box :my\_condition %\>

---

<div class="post-metadata">

**Author:** ![dburges](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7bcc69/32.png) [@dburges](https://www.ruby-forum.com/u/dburges)\
**Post date:** [April 3, 2010, 12:01am UTC](https://www.ruby-forum.com/t/trying-to-create-search-filter-persistence-using-searchlogic/185023/4 "2010-04-03T00:01:07Z")

</div>

Patrick Shainin wrote:

> For example, to add a check box that limits the search to rows with a  
> certain condition, you could define a named scope in Model.rb like this:
> 
> named\_scope :my\_condition, :conditions =\> “your sql condition here”
> 
> Then in the view form, you would include:  
> \<%= f.label :my\_condition, “Check to apply my condition” %\>  
> \<%= f.check\_box :my\_condition %\>

I forgot that f.check\_box adds a hidden field so even unchecked, the  
condition will trigger. One way to get around that is to use lambda in  
the named scope and adjust what condition gets included based on the  
check box value:

named\_scope :my\_condition, lambda { |check| “1”==check ? {:conditions=\>  
“your sql condition here”} : {}}
