# Models distance in terms of associations

**URL:** <https://www.ruby-forum.com/t/models-distance-in-terms-of-associations/163542>\
**Category:** Rails\
**Created:** [March 21, 2009, 7:24pm UTC](https://www.ruby-forum.com/t/models-distance-in-terms-of-associations/163542 "2009-03-21T19:24:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![gabriele](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/76d3ee/32.png) [@gabriele](https://www.ruby-forum.com/u/gabriele)\
**Post date:** [March 21, 2009, 7:24pm UTC](https://www.ruby-forum.com/t/models-distance-in-terms-of-associations/163542/1 "2009-03-21T19:24:30Z")

</div>

Hi,  
I was wondering if there’s a way to know the associations needed to  
reach a model from another, i.e.  
I have a Customer, I know it has some Invoices, each of these has more  
InvoiceRows which belongs to an InvoiceRowDescription, I would like to  
know if there’s a way to automagically build something like this (for  
an auto generated named scope’s join key):

{:invoices =\> {:invoice\_rows =\> :invoice\_row\_description}}

knowing just that the current model is Customer, and the last model is  
InvoiceRowDescription

Thanks,

G.

---

<div class="post-metadata">

**Author:** ![gabriele](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/76d3ee/32.png) [@gabriele](https://www.ruby-forum.com/u/gabriele)\
**Post date:** [March 21, 2009, 7:59pm UTC](https://www.ruby-forum.com/t/models-distance-in-terms-of-associations/163542/2 "2009-03-21T19:59:03Z")

</div>

On Mar 21, 6:23 pm, Gabriele T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Hi,  
> I was wondering if there’s a way to know the associations needed to  
> reach a model from another, i.e.  
> I have a Customer, I know it has some Invoices, each of these has more  
> InvoiceRows which belongs to an InvoiceRowDescription, I would like to  
> know if there’s a way to automagically build something like this (for  
> an auto generated named scope’s join key):

There’s not something builtin (although it does sound like it would be  
a relatively simple graph traversal exercise to produce something like  
this (picking the ‘best’ path if there were multiple ones might be  
trickier)).

Fred

---

<div class="post-metadata">

**Author:** ![gabriele](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/76d3ee/32.png) [@gabriele](https://www.ruby-forum.com/u/gabriele)\
**Post date:** [March 22, 2009, 8:05am UTC](https://www.ruby-forum.com/t/models-distance-in-terms-of-associations/163542/3 "2009-03-22T08:05:27Z")

</div>

On 21 Mar, 19:58, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On Mar 21, 6:23 pm, Gabriele T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> There’s not something builtin (although it does sound like it would be  
> a relatively simple graph traversal exercise to produce something like  
> this (picking the ‘best’ path if there were multiple ones might be  
> trickier)).

Indeed there are multiple choices… it looks like an operations  
research problem… I could look at all the model\_ids’ fields in the  
last model and traverse them up to the first of the chain (the one  
which don’t belongs to some other model), if I find the current model  
in this chain, I can stop, otherwise I take other associations  
chains… in case of loops… I can consider it a dead end… but this  
optimal approach can explode in terms of computational complexity (ok,  
we are dealing with very fiew elements, possibly dozens, rarely an app  
can have more than 100 models… thus it won’t be a real problem,  
but… who knows!)
