Forum: Ruby on Rails ActiveRecord find all based on array -- need ids from array

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Josh K (Guest)
on 2006-04-07 20:18
Does anyone know if it is possible to perform an ActiveRecord find by
passing in an array?  I would like to be able to do the following:

  MyModel.find(:all, :conditions => ["user_id in (?)", users])

The problem I run into is that the returned SQL contains the to_s()
output of the User object, not the ids of those users.  Is it possible
to change this behavior?  I attempted to override the to_s() method in
my model class, but to no avail (also tried to_param()).

Thanks!
Mark Van H. (Guest)
on 2006-04-07 20:24
(Received via mailing list)
i've never used the in caluse in the conditions for active record, but
it it
truely does take an array an put it in there properly, this is most
likely
what you are looking for..

MyModel.find(:all, :conditions => ["user_id in (?)", users.collect{ |u|
u.id} ])
Josh K (Guest)
on 2006-04-07 20:28
That worked...thanks!!

Mark Van H. wrote:
> i've never used the in caluse in the conditions for active record, but
> it it
> truely does take an array an put it in there properly, this is most
> likely
> what you are looking for..
>
> MyModel.find(:all, :conditions => ["user_id in (?)", users.collect{ |u|
> u.id} ])
This topic is locked and can not be replied to.