Darkstarone
Darkstarone

Reputation: 4730

Check whether activerecord field is a substring of a string

So I know how to check if a query is a substring of a field:

where("field like ?", query)

But how would I do it in reverse? So that it checks if a field is a substring of the query?

E.g. if I have objects:

[123,45,678]

I want to know which ones occur in the number:

45678 = [45,678]

My current solution is:

mer = []    
Mer14.find_each do |m| 
  if query.include?(m.sequence)
    mer << m
  end

But I'm not sure if this is efficient or ideal!

Upvotes: 1

Views: 582

Answers (1)

Wizard of Ogz
Wizard of Ogz

Reputation: 12643

Try reversing the arguments

where("? LIKE field", query)

Upvotes: 1

Related Questions