Gareth Adams
2007-Oct-02 14:42 UTC
Finding "has_many :through" items without any associated items
Hi guys, I have a class Foo has_many :foo_bars has_many :bars, :through => :foo_bars end What''s the simplest way to find all Foos that don''t have any Bars? I know I could go through some convoluted process like finding all Foo ids and then finding all foo_ids from the foo_bars table and subtracting those, but it seems like there should be a simple query to do this. Any thoughts? Thanks in advance --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2007-Oct-02 15:02 UTC
Re: Finding "has_many :through" items without any associated items
Foo.find :all, :include => foo_bars, :conditions => "foo_bars.id is NULL" maybe (or write out the left outer join on foo_bars, either way is good) Fred On 2 Oct 2007, at 15:42, Gareth Adams wrote:> > Hi guys, > > I have a > > class Foo > has_many :foo_bars > has_many :bars, :through => :foo_bars > end > > What''s the simplest way to find all Foos that don''t have any Bars? > > I know I could go through some convoluted process like finding all > Foo ids and > then finding all foo_ids from the foo_bars table and subtracting > those, but it > seems like there should be a simple query to do this. Any thoughts? > > Thanks in advance > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---