Skip to content
Easily search you ActiveRecord models with a simple query language using a named scope.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


Scoped search

The scoped_search Rails plugin makes it easy to search your ActiveRecord models. Searching is performed using a query string, which should be passed to the named_scope search_for. Based on a definition in what fields to look, it will build query conditions and return those as a named scope.


The recommended method to enable scoped_search in your project is adding the scoped_search gem to your environment. Add the following code to your Rails configuration in config/environment.rb: do |config|
  config.gem 'wvanbergen-scoped_search', :lib => 'scoped_search',
                 :source => ''

Run sudo rake gems:install to install the gem.

Alternatively, install scoped_search as a Rails plugin:

script/plugin install git://


Scoped search requires you to define the fields you want to search in:

class User < ActiveRecord::Base
  scoped_search :on => [:first_name, :last_name]

For more information about options and using fields from relations, see the project wiki on search definitions:

Now, the search_for scope is available for queries. You should pass a query string to the scope. This can be empty or nil, in which case all no search conditions are set (and all records will be returned).

User.search_for('my search string').each { |user| ... }

The result is returned as named_scope. Because of this, you can actually chain the call with other scopes, or with will_paginate. An example:

class Project < ActiveRecord::Base
  searchable_on :name, :description
  named_scope :public, :conditions => {:public => true }

# using chained named_scopes and will_paginate in your controller
Project.public.search_for(params[:q]).paginate(:page => params[:page], :include => :tasks)

More information about usage can be found in the project wiki:

Query language

The search query language is simple, but supports several constructs to support more complex queries:


require every word to be present, e.g.: some search keywords


use quotes for multi-word phrases, e.g. "police car"


look for “everything but”, e.g. police -uniform, -"police car", police NOT car

logical keywords

make logical constructs using AND, OR, &&, ||, &, | operators, e.g. uniform OR car, scoped && search


to structure logic e.g. "police AND (uniform OR car)"

comparison operators

to search in numerical or temporal fields, e.g. > 22, < 2009-01-01

explicit fields

search only in the given field. e.g. username = root, created_at > 2009-01-01

NULL checks

using the set? and null? operator with a field name, e.g. null? graduated_at, set? parent_id

A complex query example to look for Ruby on Rails programmers without cobol experience, over 18 years old, with a recently updated record and a non-lame nickname:

("Ruby" OR "Rails") -cobol, age >= 18, updated_at > 2009-01-01 && nickname !~ l33t

For more info, see the the project wiki:

Additional resources


This plugin is released under the MIT license. Please contact weshays ( or wvanbergen ( for any questions.

Something went wrong with that request. Please try again.