Trying to create search filter persistence using SearchLogic

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!

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

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 %>

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"} : {}}