ActsAsWildSearchable
This plugin allows you to search in your models on each of its attributes using wild cards. The default behavior is to chain the search parameters using “or” and wild cards in both sides. This behavior can be changed by adding the :clause=>“and” and the :left=>true or :right=>true parameters
GEM Location
gem 'act_as_wild_searchable', :git => 'http://github.com/canimus/acts_as_wild_searchable-0.0.1.gem'
Installation
bundle install
Tested Versions
-
Rails 3.2.3
Usage
Query Basics
-
the
_like?
methodConsider that you model has the name attribute
User.name_like? "John"
-
the
chain
capabilitiesTo chain various queries just add another query after the usage of the like? method
User.name_like?("John").where(:id => 1)
-
the
multiple_search
You can add as many clauses and by default will be added using an “or” clause
User.name_like? "John","David", "Peter" # Produces where (name like ? or name like ? or name like ?, "%John%", "%David%", "%Peter%")
-
the
left
You can position the wild card with a hash addition to the call
User.name_like? "John","David", :left => true # Produces where (name like ? or name like ?, "%John", "%David")
-
the
right
You can position the wild card with a hash addition to the call
User.name_like? "John","David", :right => true # Produces where (name like ? or name like ?, "John%", "David%")
-
the
clause
You can change the default behavior for the wild card search by addint the clause hash
User.name_like? "John","David", :clause => "and" # Produces where (name like ? and name like ?, "%John%", "%David%")
General configuration options
You can configure the following attributes that will locate the wild card for your search
left # false by default
right # false by default
clause # "or" by default