How to search string in Ruby on Rails?
For example, all records with columns containing "text".
Does Active Record have a method for it, or do I need to use SQL "LIKE" for it?
Sql likecan be very inefficient in some cases, for example in many cases in MySQL . I recommend using some full-text indexing software such as Sphinx, Xapian or Lucene.
like
Model.find(:all, :conditions => ['name LIKE ?', "%#{tag}%"])
Where tag is your variable containing the string
as per @bjg comment: -
Or in Rails 3 you have to write this as
Model.where(["name LIKE :tag", {:tag => tag}])
finder -
Arel:
Model.match(:name => tag)
, .
db, postgres . . , PG - . http://tenderlove.github.com/texticle/
, act_as_ferret , ,
ActsAsFerret::define_index( 'my_index', :models => { SomeModel => { :fields => { :name => { :boost => 4, :store => :yes, :via => :ferret_title }, :foo => { :store => :no, :index => :untokenized }, :baz => { :store => :yes, :via => :ferret_content } } } } )
act_as_ferret , RAILS_ROOT/config/aaf.rb
find this tutorial, it looks good full-text search in ruby on rails 3 (Ferret)
Github source: http://github.com/jkraemer/acts_as_ferret/tree/rails3
Original plugin: http://ferret.davebalmain.com/trac/wiki/FerretOnRails