Advanced string comparison using

47 views
Skip to first unread message

Tal Sh

unread,
Nov 17, 2014, 12:19:32 PM11/17/14
to rubyonra...@googlegroups.com
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/.

Vivek Sampara

unread,
Nov 18, 2014, 1:43:14 AM11/18/14
to rubyonra...@googlegroups.com
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"," "] )


--
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 rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
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.

Tal Sh

unread,
Nov 18, 2014, 2:44:39 AM11/18/14
to rubyonra...@googlegroups.com
Thanks!

But I might have no been clear, that the string I want to find is
actually "hello".
The substrings are strings I want to *ignore* when searching..

Vivek Sampara

unread,
Nov 18, 2014, 2:53:55 AM11/18/14
to rubyonra...@googlegroups.com
Then you can simply use 

Post.where("name ILIKE %?%",  "hello")

or postgres ( can insensitive ) 



Post.where("name ILIKE %?%",  "hello")

Tal Sh

unread,
Nov 18, 2014, 3:15:31 AM11/18/14
to rubyonra...@googlegroups.com
That wouldn't catch "hellXXo"...

Vivek Sampara

unread,
Nov 18, 2014, 4:27:06 AM11/18/14
to rubyonra...@googlegroups.com

Hey sorry for misunderstanding it. You will have to use native database specific regex or pure ruby based expressions and select im a bit bad at regular expressions but i should look something like this  :)  

Post.all.select{|x| x.title ~= /[hello]*/ }





--
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 rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.

Tal Sh

unread,
Nov 18, 2014, 6:04:57 PM11/18/14
to rubyonra...@googlegroups.com
Thanks, I'll try that.
Reply all
Reply to author
Forward
0 new messages