Adding search to ActiveRecord, Rails model
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.
Advertisement
Trackbacks & Pingbacks