# Populating parent relationships automagically

**URL:** <https://www.ruby-forum.com/t/populating-parent-relationships-automagically/176405>\
**Category:** Rails\
**Created:** [September 30, 2009, 6:27am UTC](https://www.ruby-forum.com/t/populating-parent-relationships-automagically/176405 "2009-09-30T06:27:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 30, 2009, 6:27am UTC](https://www.ruby-forum.com/t/populating-parent-relationships-automagically/176405/1 "2009-09-30T06:27:58Z")

</div>

I would like to populate parent relationships when I load children  
objects. I have a relatively complex object graph. Something like:  
Thing \<1-_\> Components \<1-_\> Assembly \<_-_\> Parts

The notion is that we allow a Thing to be designed with ‘construction  
issues’, but can’t be ‘manufactured’ until it doesn’t have any issues.  
There can be issues at any level. A Thing can have too many  
components. A component can have the wrong kinds of Assemblies. So  
far, so good.

The problem comes in where there are complex grouping rules. There  
are grouping rules for Parts that depend on how many of the same Part  
exist within the entire Thing, for example.

When I fetch, I do something like  
Thing.find(some\_id, :includes =\> {:components =\> {:assmeblies  
=\> :parts}})  
and that works fine.

But when I want to display some of the errors associated with a Part,  
I have to reach back up to Thing to find some of the issues.

part.assembly.component.thing will return a different object for every  
part.

So I’m interested in a plugin or some code that will populate the  
parent relationships when the children objects are loaded. Then I’ll  
be able to traverse relationships up and down without proliferating  
objects.

Or other architectural suggestions.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 30, 2009, 7:49am UTC](https://www.ruby-forum.com/t/populating-parent-relationships-automagically/176405/2 "2009-09-30T07:49:55Z")

</div>

On Sep 30, 11:26 am, “removed\_email\_address@domain.invalid” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I would like to populate parent relationships when I load children  
> objects. I have a relatively complex object graph. Something like:  
> Thing \<1-_\> Components \<1-_\> Assembly \<_-_\> Parts

> part.assembly.component.thing will return a different object for every  
> part.

Most likely you are maintaining a many to many relationship table  
between \<% :parts %\> and \<% :assemblies %\>. That means a single Part  
can be associated with multiple Assembly.

As you wrote: \<% part.assembly.component.thing %\>

In the above call \<% part.assemble %\> will return array of assembles  
and you can not call \<% .component %\> on that array.

You need to distinguish between the Assemblies of a single Part to  
reach up to its’ Thing.  
Would you please give some of your sample associations and the  
scenario where you need to call \<% part.assembly.component.thing %\>?  
Hope i can provide something more specific.

Thank you

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 30, 2009, 5:44pm UTC](https://www.ruby-forum.com/t/populating-parent-relationships-automagically/176405/3 "2009-09-30T17:44:08Z")

</div>

On Sep 29, 10:48 pm, Samiron [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> between \<% :parts %\> and \<% :assemblies %\>. That means a single Part  
> can be associated with multiple Assembly.
> 
> As you wrote: \<% part.assembly.component.thing %\>
> 
> In the above call \<% part.assemble %\> will return array of assembles  
> and you can not call \<% .component %\> on that array.

You’re right, I botched my analogy. It is assembly that is reaching  
up.

> Would you please give some of your sample associations and the  
> scenario where you need to call \<% part.assembly.component.thing %\>?  
> Hope i can provide something more specific.

Supposing that  
def Thing#assemblies  
components.map(&:assemblies).flatten.uniq  
end

def Assembly#manufacturing\_issues  
issues = []  
…  
if component.thing.assemblies.map(&:type).include?(conflicting\_type)  
issues \<\< “Can not have a #{conflicting\_type} in the same thing.”  
end  
…  
issues  
end

Here we have an assembly reaching up to its Thing to find out if any  
of its peers is of conflicting type. Keep in mind that I want this  
issue to be displayed with the Assembly - so this is where I put the  
logic. Also it seems reasonable to keep it here since it is knowledge  
of the Assembly types that defines whether or not it conflicts.

Since parent relationships are not populated, we’re going to get a  
different Thing - which means that Thing is going to need to load  
_all_ its assemblies just to see if there is one that conflicts. I’d  
like to be able to traverse the object graph and get to the Thing that  
caused this Assembly to load - that way I would be using the same  
objects; no additional loads needed…
