This bit of code finds several groups; all those which have
entity_id’s of ‘1’ OR ‘2’ OR ‘3’, as you would expect:
ee = [ 1, 2, 3 ]
groups = Group.find(:all, :conditions => “entity_id in (” + ee.join
(",") + “)”)
But this bit of code only finds groups which have entity_id’s of ‘1’:
ee = [ 1, 2, 3 ]
groups = Group.find(:all, :conditions => [ “entity_id in (?), ee.join
(”,") ] )
Why is that?
Evidently, if I switch the order like this:
ee = [ 3, 2, 1 ]
I only get groups with an entity_id of ‘3’. So clearly if I specify
the parameter like this ‘(?)’, I get only the first element from the
‘ee’ list included in the where clause. Why?
Thanks for the suggestions. The plot thickens. If I do this:
ee = [ 4, 9 ]
Then the suggestions above both work:
groups = Group.find(:all, :conditions => [ “entity_id in (?)”, ee] )
or
groups = Group.find_all_by_entity_id(ee)
return what I would expect. However, ee is created like this:
ee = User.find(session[:user_id]).entityadministrators.find(:all).map
{ |e| [e.id]}
My understanding was that that would create an array of entity_id’s.
Indeed, it does seem to and if I join the array entries together with
a comma and then use that result as a string condition in my groups
query, it works. But if I just try to use the array as is, with the
suggestions above, I get no rows returned.
So, the question becomes, what’s the difference between:
ee = User.find(session[:user_id]).entityadministrators.find(:all).map
{ |e| [e.id]} # which returns a 4 and and a 9