find if child element is nil

@childless_parents = Parent.find_by_sql("select * from parents where id not in (select distinct parent_id from parents")

oops, just noticed that the inner nested query is missing a close bracket.

At the expense of some CPU:

Model.find(:all).select { |m| !m.children.blank? }

Rather than checking to see if they're nil, because != nil, use .blank? which checks for nil and emptiness. For the children children...

Model.find(:all).select { |m| !m.children.map(&:children).flatten.empty? } \

Parent.find :all, :include => :children, :conditions => "child_table.id IS NULL"

should work, since :include generates left outer joins.

Fred

At the expense of some CPU:

Model.find(:all).select { |m| !m.children.blank? }

Rather than checking to see if they're nil, because != nil, use .blank? which checks for nil and emptiness. For the children children...

Model.find(:all).select { |m| !m.children.map(&:children).flatten.empty? }