New version of Scoped search
After an almost complete rewrite, I am proud to present version 2.0 of scoped_search, the ActiveRecord plugin that makes it easy to find records using a simple query language. This new version support a new query language that supports more complex constructs, and can therefore be used to conduct more fine-grained queries on your models.
New query language
- Logical operators: AND (&, &&), OR (|, ||) and NOT (!, -) operators, and parentheses to structure the boolean logic:
police AND (car || uniform), -"village people". By default, the AND operator is used to combine different segments of your query. - Comparison operators: the most common comparison operators are supported, and to what you expect on integer and date field.
- Explicit field support: only search in the specified field instead of all fields:
age >= 21,created < 2009-01-01,username != "root". - Check for NULL fields:
null? parent,set? error_message - Commas are supported to separate the different parts of the query.
More information about the query language can be found in the project wiki on GitHub.
New definition syntax
The new version supports a new syntax to define what fields of your model can be searched and in what cases. An example:
class User < ActiveRecord::Base belongs_to :account_type scoped_search :on => [:first_name, :last_name] scoped_search :on => :created_at, :alias => :created, :only_explicit => true scoped_search :in => :account_type, :on => [:name, :description] end
After the fields have been defined, the search_for method can be used to search your models using a named scope, just like it was before. The project wiki has more information about this new syntax. The search syntax itself hasn’t changed:
@users = User.search_for(params[:q]).paginate(page => params[:page])
Installation or upgrade
Include the gem in your environment.rb configuration and run rake gems:install to install it:
config.gem 'scoped_search', :source => 'http://gemcutter.org'
Backwards compatibility
The new version has a new syntax to define the fields that can be searched with a query. This new syntax gives you more fine-grained control over the queries that will be generated, so I urge you to adopt this new syntax. However, the old searchable_on syntax is still available for backwards compatibility.
Please contact me if you have any issues with the new version.
(Updated with new gemcutter installation instructions.)






September 18th, 2009 at 10:20 am
I have just tried to install your plugin, and it works great, thanks.
A small thing though is that I get deprecation warnings when using it:
/Library/Ruby/Gems/1.8/gems/wvanbergen-scoped_search-2.0.0/lib/scoped_search/definition.rb:23: warning: Object#type is deprecated; use Object#class
September 18th, 2009 at 11:04 am
After looking into the code, I discovered that this message probably means that you defined scoped search on a field name that could not be found. It tries to get the type of the column (column.type), but column probably is nil, so it tries to execute nil.type, triggering the deprecation warning.
Can you post or send me some more information (scoped search invocation, the table definition, and your rails version) so I can investigate further?