I''m trying to do an IN query, where I have an array of user objects imaginatively named ''users'': :conditions=>["user_id IN (?)", users.collect{|u| u.id }.join('','')] this ends up with: ... IN(''1,2,3'') ... Obviously I want this to be: ... IN(1,2,3) ... or even ... IN(''1'', ''2'', ''3'') ... How do I get ActiveRecord to stop quoting the whole thing? I thought passing in the object array itself did the trick - the documentation says as such, giving the following example: Person.find(:all, :conditions => [ "category IN (?)", categories]) When I try this, the array is printed and my query looks something like: IN (''--- !ruby/object:User \nattributes: ..... Any ideas? Thanks in advance - I''ll carry on Googling and see what I can turn up! -- R.Livsey http://livsey.org
If categories is an array of numbers (think user id''s) your second idea will work: users = [ 1,2,3,4,5] Person.find :all, :conditions=>[''id IN (?)'', users] but since you have actual user objects you could simplify and say: users = [ user1, user2, user3 ] Person.find_all_by_id users.map{ |u| user.id } Hope this helps, Zach Richard Livsey wrote:> I''m trying to do an IN query, where I have an array of user objects > imaginatively named ''users'': > > :conditions=>["user_id IN (?)", users.collect{|u| u.id }.join('','')] > > this ends up with: > > ... IN(''1,2,3'') ... > > Obviously I want this to be: > > ... IN(1,2,3) ... > or even > ... IN(''1'', ''2'', ''3'') ... > > How do I get ActiveRecord to stop quoting the whole thing? > > I thought passing in the object array itself did the trick - the > documentation says as such, giving the following example: > > Person.find(:all, :conditions => [ "category IN (?)", categories]) > > When I try this, the array is printed and my query looks something like: > > IN (''--- !ruby/object:User \nattributes: ..... > > Any ideas? > > Thanks in advance - I''ll carry on Googling and see what I can turn up! >
Superb - thanks! Just had to take out the .join('','') and all works a charm. Thanks again. -- R.Livsey http://livsey.org zdennis wrote:> If categories is an array of numbers (think user id''s) your second idea > will work: > > users = [ 1,2,3,4,5] > Person.find :all, :conditions=>[''id IN (?)'', users] > > but since you have actual user objects you could simplify and say: > > users = [ user1, user2, user3 ] > Person.find_all_by_id users.map{ |u| user.id } > > Hope this helps, > > Zach > > Richard Livsey wrote: >> I''m trying to do an IN query, where I have an array of user objects >> imaginatively named ''users'': >> >> :conditions=>["user_id IN (?)", users.collect{|u| u.id }.join('','')] >> >> this ends up with: >> >> ... IN(''1,2,3'') ... >> >> Obviously I want this to be: >> >> ... IN(1,2,3) ... >> or even >> ... IN(''1'', ''2'', ''3'') ... >> >> How do I get ActiveRecord to stop quoting the whole thing? >> >> I thought passing in the object array itself did the trick - the >> documentation says as such, giving the following example: >> >> Person.find(:all, :conditions => [ "category IN (?)", categories]) >> >> When I try this, the array is printed and my query looks something like: >> >> IN (''--- !ruby/object:User \nattributes: ..... >> >> Any ideas? >> >> Thanks in advance - I''ll carry on Googling and see what I can turn up! >> > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > >
You can also use a handy rails shortcut: categories.collect { |c| c.id } Can be written as: categories.collect(&:id) http://dev.rubyonrails.org/browser/trunk/activesupport/lib/active_support/core_ext/symbol.rb -Jonathan. On 4/6/06, Richard Livsey <richard@livsey.org> wrote:> > Superb - thanks! > > Just had to take out the .join('','') and all works a charm. > > Thanks again. > > -- > R.Livsey > http://livsey.org > > zdennis wrote: > > If categories is an array of numbers (think user id''s) your second idea > > will work: > > > > users = [ 1,2,3,4,5] > > Person.find :all, :conditions=>[''id IN (?)'', users] > > > > but since you have actual user objects you could simplify and say: > > > > users = [ user1, user2, user3 ] > > Person.find_all_by_id users.map{ |u| user.id } > > > > Hope this helps, > > > > Zach > > > > Richard Livsey wrote: > >> I''m trying to do an IN query, where I have an array of user objects > >> imaginatively named ''users'': > >> > >> :conditions=>["user_id IN (?)", users.collect{|u| u.id }.join('','')] > >> > >> this ends up with: > >> > >> ... IN(''1,2,3'') ... > >> > >> Obviously I want this to be: > >> > >> ... IN(1,2,3) ... > >> or even > >> ... IN(''1'', ''2'', ''3'') ... > >> > >> How do I get ActiveRecord to stop quoting the whole thing? > >> > >> I thought passing in the object array itself did the trick - the > >> documentation says as such, giving the following example: > >> > >> Person.find(:all, :conditions => [ "category IN (?)", categories]) > >> > >> When I try this, the array is printed and my query looks something > like: > >> > >> IN (''--- !ruby/object:User \nattributes: ..... > >> > >> Any ideas? > >> > >> Thanks in advance - I''ll carry on Googling and see what I can turn up! > >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060405/c46540a3/attachment.html
User.find(1,2,3,4) On 4/5/06, Richard Livsey <richard@livsey.org> wrote:> I''m trying to do an IN query, where I have an array of user objects > imaginatively named ''users'': > > :conditions=>["user_id IN (?)", users.collect{|u| u.id }.join('','')] > > this ends up with: > > ... IN(''1,2,3'') ... > > Obviously I want this to be: > > ... IN(1,2,3) ... > or even > ... IN(''1'', ''2'', ''3'') ... > > How do I get ActiveRecord to stop quoting the whole thing? > > I thought passing in the object array itself did the trick - the > documentation says as such, giving the following example: > > Person.find(:all, :conditions => [ "category IN (?)", categories]) > > When I try this, the array is printed and my query looks something like: > > IN (''--- !ruby/object:User \nattributes: ..... > > Any ideas? > > Thanks in advance - I''ll carry on Googling and see what I can turn up! > > -- > R.Livsey > http://livsey.org > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Tobi http://shopify.com - modern e-commerce software http://typo.leetsoft.com - Open source weblog engine http://blog.leetsoft.com - Technical weblog