find and :conditions question

Hello

lets say i have model with :name string field, how to write a "find :all, :condition => ???" to find records that :name fields includes certain string... IRB funcionality example, hope that makes it more clear

irb > str = "112369" => "112369" irb > str.include? "112" => true irb > str.include? "11" => true irb > str.include? "99" => false

Presuming you wanted to filter on the condition in the database, read

  Active Record Query Interface — Ruby on Rails Guides

section 2.1 , 2.2 ...

Short answer

  match_string = "112"   MyModel.where("name LIKE '%?%'", match_string)

But this is quite flawed for performance on the database ... Read-up on "full text search" to understand those issues.

If you wanted to filter in Ruby, which would only make sense for "small" amounts of records (< 100 ?), you could do:

  match_string = "112"   matcher = Regexp.new(match_string)   MyModel.all.select{|o| o.name =~ matcher}

Read-up on Regular expressions and Enumerable functionality for this.

HTH,

Peter