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

--
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@googlegroups.com.
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.

Search Discussions

  • Peter Vandenabeele at Mar 1, 2012 at 11:57 am

    On Thu, Mar 1, 2012 at 12:23 PM, Marcin S 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@googlegroups.com.
    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.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedMar 1, '12 at 11:23a
activeMar 1, '12 at 11:57a
posts2
users2
websiterubyonrails.org
irc#RubyOnRails

2 users in discussion

Marcin S: 1 post Peter Vandenabeele: 1 post

People

Translate

site design / logo © 2022 Grokbase