How to search for text with an active record in Ruby on Rails 3?

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?

+5
source share
5 answers

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.

+8
source
 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 -

+11

Arel:

Model.match(:name => tag)

, .

+3

db, postgres . . , PG - . http://tenderlove.github.com/texticle/

+3

, 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

+2
source

All Articles