0

I have the following query in Squeel:

languages = where{ (name == 'English') | 
 (name == 'Spanish')  | 
 (name == 'German')  | 
 (name == 'French')  | 
 ...
}

Which works fine but I was wodering if there was a way to iterate through an array with those values to make it easier to add and remove languages. Say I created and array:

languages_array = %w[ English Spanish French ... ]

Is there any way to iterate with Squeel in a Where block?

I've tried everything I can think of without success.

Thanks.

kakubei
  • 5,321
  • 4
  • 44
  • 66

2 Answers2

3

I think this should work

where{name.in(languages_array)}
Santhosh
  • 28,097
  • 9
  • 82
  • 87
0

You don't need squeel for this type of query: where(name: languages_array) will do what you need - it'll translate to

WHERE name IN ('English', 'Spanish', 'French', ...)

which is probably what you need.

eugen
  • 8,916
  • 11
  • 57
  • 65
  • That's true, but when you are using a layer like *squeel* I guess it makes sense to use it for everything, otherwise you'll end up with a funny mixture of queries. – tokland Jul 10 '13 at 11:02
  • This is true, I'm trying to stick to one or the other but it's proving difficult. I even have had to write some straight `find_by_sql` scary queries because I found no way to do them in Rails. – kakubei Jul 10 '13 at 12:31