Francis.Beauchamp Francis.Beauchamp - 1 year ago 78
Ruby Question

Sequel Grep using variables as matcher

I'd like to do a query using the Sequel's

object. I'd like the pattern to match certain variables value like so:

Models::Show.grep(:name, %w'#{var1} #{var2}')

But unfortunately, the Models.grep instance method seems not to be working this way. It query as following:

SELECT * FROM "shows"
WHERE (("name" LIKE '#{titre_fr}' ESCAPE '\')
OR ("name" LIKE ' {titre_en}' ESCAPE '\'))

Is there a way to make the function work with variables ?

Answer Source

Just do as below :

Models::Show.grep(:name, %W(#{var1} #{var2}))

Demo example :

a = 2
b = 3
%W(#{a} #{b}) # => ["2", "3"]

Read this also Ruby arrays: %w vs %W.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download