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...)? -- Posted via http://www.ruby-forum.com/.
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- s.net> wrote:> 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...)? > -- > Posted viahttp://www.ruby-forum.com/.
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. -- Posted via http://www.ruby-forum.com/.
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-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org http://www.schuerig.de/michael/