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

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs