Skip to content

Adding search to ActiveRecord, Rails model

July 11, 2011

Adding search to a rails model using the scoped search rails plugin is a simple declaration of the desired search syntax.

In an ActiveRecored driven model, we will need to add the following declaration:

class book < ActiveRecord
scoped_search :on => :title

This declaration will enable search on a field called ‘title’ in the database table that is referenced by the current model.

Two search syntax will be available to the user:  free-text, for example “lorem”,  and specific search term, for example  “title ~ ipsum”.

To enable searching for the current model by conditions on a property of a related model use the following declaration:

scoped_search :in => author, :on => :name

To change the searched property name use the following declaration

scoped_search :in => author, :on => :name,  :rename => author

The result is a search syntax where a user can type “author = oz” and get the list of books by a writer named oz.


From → Developers

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: