Does an object have children - method

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?

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

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