Josh K
2006-Apr-07 16:18 UTC
[Rails] ActiveRecord find all based on array -- need ids from array
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! -- Posted via http://www.ruby-forum.com/.
Mark Van Holstyn
2006-Apr-07 16:23 UTC
[Rails] ActiveRecord find all based on array -- need ids from array
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} ]) On 4/7/06, Josh K <jkahn_117@yahoo.com> wrote:> > 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! > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Mark Van Holstyn mvette13@gmail.com http://lotswholetime.com -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060407/5f410711/attachment.html
Josh K
2006-Apr-07 16:29 UTC
[Rails] Re: ActiveRecord find all based on array -- need ids from ar
That worked...thanks!! Mark Van Holstyn 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} ])-- Posted via http://www.ruby-forum.com/.