Find if child element is nil

so i have a parent and child model, i need to get all the entries in my
parent model that don’t yet have a child. i can do a find all and
iterate through all the buggers, but this is a little cpu intensive. How
can i get something like this pseudo code to work??

@parents = Parent.find(:all, :conditions => [ :children => nil ])

for bonus points you can do the same thing with a parent, child , and
child’s child.

@parents = Parent.find(:all, :include => :children, :conditions =>
[:childrens-children => nil ])

Thanks,
Richard
http://www.slangslang.com

some code would be nice. Especially if you give bonus points!

if the parent belongs_to child or viceversa, then you can use your
format above. However, if you have a join table (HABTM or HMT) then
you will need to do a find based on that join table

JoinTable.find(:all, :conditions => {:child_id => nil })

j

On Jul 9, 3:40 am, Richard S. <rails-mailing-l…@andreas-

You could use counter_cache in the association, though this will require
you to add a children_count to your parents table.

Alternatively, you could use a bit of sql:

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

This assumes that parents and children are actually seperate classes:
if they’re the same class, and they’re all in the ‘parents’ table then
it’s

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

Which is like saying 'give me all the parents who aren’t a parent to a
child".
The second approach can be extended to grandchildren as well, by finding
all the grandparents and subtracting them from the total list of
parents.

If parents, children, and grandchildren are all the same class of object
then you should have a look at the acts_as_tree plugin, which is useful
for this sort of thing.

@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.

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? } \

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? }

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