try this

arr = ["XX","YY","AB"," "]

def self.custom_search(arr = [])
  query = []
  arr.each do |term|
    query += "title LIKE %#{term}%"
  end
  where(query.join(" OR "))
end

If you're using postgres DB

def self.custom_search(arr = [])
  query = []
  arr.each do |term|
    query += "title ILIKE %#{term}%"
  end
  where(query.join(" OR "))
end

Paste this method in any model and call it using like this

Post.custom_search(["XX","YY","AB"," "] )


On Mon, Nov 17, 2014 at 10:47 PM, Tal Sh <[email protected]> wrote:

> Hi guys,
>
> I'm looking for a way to pick up database records by comparing my string
> to the database record's string.
>
> The catch is that I have to "clean up" the database strings (not change
> the record, just for the purpose of this query).
>
> The basic query is:
>
> Book.where(“title = ?”,”hello”)
>
> And I have an array of substrings e.g.
>
> ["XX","YY","AB"," "]  (the last one is space)
>
> So a record where “hellXX o” or “XX  YY  hellXX o YY   ” would be picked
> up.
>
> Any ideas? (performance is not important)
>
> Thanks..!
>
> --
> Posted via http://www.ruby-forum.com/.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/rubyonrails-talk/72a9ca1464a08150e2fcda643ae7f08b%40ruby-forum.com
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rubyonrails-talk/CAFKVRj9XN7YYu%2BUBArL5pvG1Vag1%2BLtLr6ZA%3D_R95OWs4xgdFA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to