On Thu, Mar 1, 2012 at 12:23 PM, Marcin S
<msporysz06-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> 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
http://guides.rubyonrails.org/active_record_querying.html
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
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.