Forum: Ruby on Rails named_scope on has_many, find all records that don't have elements in that association

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.
31fae7b624e38edb7b78753b71bacff3?d=identicon&s=25 Gabriele Tassoni (Guest)
on 2009-01-11 23:33
(Received via mailing list)
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...
53be54e5db4dc58e4980db5a8255621b?d=identicon&s=25 Harold (Guest)
on 2009-01-12 00:21
(Received via mailing list)
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...@gmail.com>
This topic is locked and can not be replied to.