# Does an object have children - method

**URL:** <https://www.ruby-forum.com/t/does-an-object-have-children-method/127281>\
**Category:** Rails\
**Created:** [January 27, 2008, 7:12pm UTC](https://www.ruby-forum.com/t/does-an-object-have-children-method/127281 "2008-01-27T19:12:22Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jimmypalmer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bbe5ce/32.png) [@jimmypalmer](https://www.ruby-forum.com/u/jimmypalmer)\
**Post date:** [January 27, 2008, 7:12pm UTC](https://www.ruby-forum.com/t/does-an-object-have-children-method/127281/1 "2008-01-27T19:12:22Z")

</div>

Is there a technique/method that tells whether an object has a child or  
many children or no children?

for example

parent has\_many :children\_1  
parent has\_many :children\_2

Given the parent, how do I get the two collections of children?

---

<div class="post-metadata">

**Author:** ![jimmypalmer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bbe5ce/32.png) [@jimmypalmer](https://www.ruby-forum.com/u/jimmypalmer)\
**Post date:** [January 28, 2008, 11:59am UTC](https://www.ruby-forum.com/t/does-an-object-have-children-method/127281/2 "2008-01-28T11:59:47Z")

</div>

in this case parent.children\_1 is an array and you can simply use  
if parent.children\_1 then  
…  
end

or:  
parent.children\_1.each do |child|  
…  
end

since it’s nil if there are no children

or use:  
parent.children\_1.length  
parent.children\_1.size  
to get the actual number of children
