0

I'd like to do a query using the Sequel's Models 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 ?

2 Answers 2

1

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.

8
  • I just tried it, getting the same result. is the W case sensitive ? Jan 9, 2014 at 19:49
  • @Francis.Beauchamp Can you give the method documentation link..? Jan 9, 2014 at 19:50
  • Edited my first comment. You will find the doc here sequel.jeremyevans.net/rdoc/classes/Sequel/… Jan 9, 2014 at 19:51
  • @Francis.Beauchamp what is var1 and var2 ? Jan 9, 2014 at 19:53
  • Its a variable inside an array. But in the end, I think my error was not making the w upper-case. Changed it and now it seems to work as intended. Jan 9, 2014 at 19:55
0

Your code

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

Is using single quotes. Change to double quotes and it will work

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.