Forum: Ruby on Rails Multi level asociations

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Aed8f05ec7f50039f47bc16536fa06f0?d=identicon&s=25 Andrei Teodorescu (tandrei)
on 2009-05-10 00:40
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...)?
2dddec0f7717cae77ac6bceede6be5bc?d=identicon&s=25 Ram (Guest)
on 2009-05-11 11:32
(Received via mailing list)
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-
D188e591eac11021329b8821a5f954c7?d=identicon&s=25 Ar Chron (railsdog)
on 2009-05-11 15:22
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.
Bce1d1b7c3ec7b577dcb42e254899e6b?d=identicon&s=25 Michael Schuerig (Guest)
on 2009-05-11 15:56
(Received via mailing list)
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/mschuerig/rails_sql_views

Michael

--
Michael Schuerig
mailto:michael@schuerig.de
http://www.schuerig.de/michael/
This topic is locked and can not be replied to.