Gabriele Tassoni
2009-Jan-11 22:32 UTC
named_scope on has_many, find all records that don''t have elements in that association
Hi, I have a Customer model that has_many :addresses, is there a way (via named scope or any other method that''s ... ehm ... model side) to obtain all the Customers that don''t have any addresses at all (for which the associated array would be empty)? Thank you for the answer... --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Harold
2009-Jan-11 23:21 UTC
Re: named_scope on has_many, find all records that don''t have elements in that association
Thinking in SQL, you want to do this: select * from customers left outer join addresses on customers.address_id = addresses.id where addresses.id is null. First thing that comes to mind here something like: Customer.find(:all, :include => :address, :conditions => ''addresses.id is null'') hope that helps, -H On Jan 11, 5:32 pm, Gabriele Tassoni <gabriele.tass...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi, > I have a Customer model that has_many :addresses, is there a way (via > named scope or any other method that''s ... ehm ... model side) to > obtain all the Customers that don''t have any addresses at all (for > which the associated array would be empty)? > > Thank you for the answer...--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---