jlapier/searchable_by
Folders and files
| Name | Name | Last commit date | ||
|---|---|---|---|---|
Repository files navigation
SearchableBy
============
Adds a search method to query your ActiveRecord models:
searchable_by :field_a, :field_b, :field_etc
YourModel.search 'some search words'
Examples
========
# declare with searchable_by
class User < ActiveRecord::Base
enables the "search" method and by default searches 'login' and 'email' columns
searchable_by :login, :email
end
# search into associations
class User < ActiveRecord::Base
has_many :addresses
searchable_by :login, :email, :addresses => [:street, :city]
end
# simple search - based on default fields in searchable_by line
User.search 'fred'
=> [#<User>, #<User>]
# specify which fields to search on with :narrow_fields
User.search 'fred', :narrow_fields => [:email, :login, :name]
=> [#<User>, #<User>, #<User>]
# use default search fields but pass other options
User.search 'fred', :conditions => { :is_admin => true }, :limit => 2
=> [#<User>, #<User>]
# use :page option and we'll assume that you want to paginate using will_paginate
User.search 'j', :page => 2
=> [#<User>, #<User>, #<User>, #<User>, #<User>]
# by default, all search words must be found
User.search 'fred john jack susan'
=> []
# set :require_all to false if you want to do an "OR" search
User.search 'fred john jack susan', :require_all => false
=> [#<User>, #<User>, #<User>, #<User>, #<User>]
# use double quotes to identify phrases
User.search '"fred flintstone"', :narrow_fields => [:login, :full_name]
=> [#<User>]
Copyright (c) 2008-2009 Jason LaPier, released under the MIT license