# Multi level asociations

**URL:** <https://www.ruby-forum.com/t/multi-level-asociations/167601>\
**Category:** Rails\
**Created:** [May 10, 2009, 12:40am UTC](https://www.ruby-forum.com/t/multi-level-asociations/167601 "2009-05-10T00:40:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tandrei](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b77776/32.png) [@tandrei](https://www.ruby-forum.com/u/tandrei)\
**Post date:** [May 10, 2009, 12:40am UTC](https://www.ruby-forum.com/t/multi-level-asociations/167601/1 "2009-05-10T00:40:22Z")

</div>

Hi guys,  
I have this issue on RoR associations: have a ‘has many’ relationship  
cascade, on many levels. I need to display data across multiple tables,  
sort like a ‘sql table view’. I could accomplish this only up to a  
second level association, like in the below example.

class Planet \< ActiveRecord::Base  
has\_many :continents  
has\_many :countries, :through =\> :continents  
end

class Continents \< ActiveRecord::Base  
belongs\_to :planet  
has\_many :countries  
end

class Country \< ActiveRecord::Base  
belongs\_to :continent  
end

How can I do this further on, but with a larger number of tables (say A  
has many Bs that has many Cs that has many Ds etc…)?

---

<div class="post-metadata">

**Author:** ![tandrei](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b77776/32.png) [@tandrei](https://www.ruby-forum.com/u/tandrei)\
**Post date:** [May 11, 2009, 11:32am UTC](https://www.ruby-forum.com/t/multi-level-asociations/167601/2 "2009-05-11T11:32:59Z")

</div>

I may be wrong but as far as I know you just gotta keep cascading down  
that way.  
But DO keep in mind to make only those associations that are  
absolutely necessary. For eg., if you’re gonna display all countries  
in a planet only at one place, and if you are not gonna build logic  
around the planet has\_many countries, through continents association,  
then you could still get all countries in a planet by doing  
planet.continents.countries without has\_many :countries, :through  
=\> :continents.  
Then again, Im not sure if you save anything by doing that.

On May 10, 3:40 am, Andrei Teodorescu \<rails-mailing-l…@andreas-

---

<div class="post-metadata">

**Author:** ![tandrei](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b77776/32.png) [@tandrei](https://www.ruby-forum.com/u/tandrei)\
**Post date:** [May 11, 2009, 3:22pm UTC](https://www.ruby-forum.com/t/multi-level-asociations/167601/3 "2009-05-11T15:22:36Z")

</div>

Andrei Teodorescu wrote:

> How can I do this further on, but with a larger number of tables (say A  
> has many Bs that has many Cs that has many Ds etc…)?

If the result is something like a sql table view, why not make it a real  
database view, and just hook up a model. In a recent app, I did just  
that since the user wanted to see data from across 5 tables in one view.  
A sql view with a model hooked to it solved the issue nicely, and I  
create the view exactly as I wanted it, and off load all the heavy  
joining logic to the database itself.

---

<div class="post-metadata">

**Author:** ![tandrei](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b77776/32.png) [@tandrei](https://www.ruby-forum.com/u/tandrei)\
**Post date:** [May 11, 2009, 3:56pm UTC](https://www.ruby-forum.com/t/multi-level-asociations/167601/4 "2009-05-11T15:56:17Z")

</div>

On Monday 11 May 2009, Ar Chron wrote:

> Andrei Teodorescu wrote:
> 
> > How can I do this further on, but with a larger number of tables  
> > (say A has many Bs that has many Cs that has many Ds etc…)?
> 
> If the result is something like a sql table view, why not make it a  
> real database view, and just hook up a model.

If you go that way, have a look at

[http://github.com/aeden/rails\_sql\_views](http://github.com/aeden/rails_sql_views)  
[http://github.com/mschuerig/rails\_sql\_views](http://github.com/mschuerig/rails_sql_views)

Michael

–  
Michael S.  
[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[http://www.schuerig.de/michael/](http://www.schuerig.de/michael/)
