I has several model objects that flow down like a tree: Class1 has_many Class2 Class2 has_many Class3 and so on. when are the child records actually created? If I do: c1 = Class1.find(:all) are the child objects (C2s below) created then or not until i do something like: c2s = c1.C2s while iterating through the c1s? If the form, that''s cool, no problem, but If I''ve got say 1000 recs in each of the tables corresponding to the has many relationship, that could be quite a problem, right? And I''m aware I can append columns to an object to prevent it, but I may not want to do it that way. When I actually do something like c2 = c1.C2s, I will want of that attributes of those child objects. So my question is is there a way to prevent the relationship when you want to, even if it''s defined in the class? thanks! rb
Rich Brant wrote:> I has several model objects that flow down like a tree: > > Class1 has_many Class2 > Class2 has_many Class3 > > and so on. > > when are the child records actually created? > > If I do: > > c1 = Class1.find(:all) > > are the child objects (C2s below) created then or not until i do something like: > > c2s = c1.C2s while iterating through the c1s? > > > If the form, that''s cool, no problem, but If I''ve got say 1000 recs in > each of the tables corresponding to the has many relationship, that > could be quite a problem, right? And I''m aware I can append columns to > an object to prevent it, but I may not want to do it that way. When I > actually do something like c2 = c1.C2s, I will want of that attributes > of those child objects. So my question is is there a way to prevent > the relationship when you want to, even if it''s defined in the class?By default, it''s the latter behaviour - children are loaded ''lazily'', when referenced. You can get eager loading of one level of children using the :include option in your find, e.g. posts = Post.find(:all, :include => [ :author, :comments ]) See the section on Eager Loading in http://ar.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods.html regards Justin
> From: Rich Brant <rbrant@gmail.com> > > when are the child records actually created? > > If I do: > > c1 = Class1.find(:all) > > are the child objects (C2s below) created then or not until i do > something like: > > c2s = c1.C2s while iterating through the c1s? > > If the form, that''s cool, no problem, but If I''ve got say 1000 recs in > each of the tables corresponding to the has many relationship, that > could be quite a problem, right?Are you observing a performance problem at the moment ? Have you run some load testing which is indicating a performance problem for some future expect load ? I realise I''m being a little facetious, but I learned a while ago with Rails that everything I thought I knew about performance and "obvious scalability problems" was wrong. My advice, either test/measure it and optimise, or don''t worry about it at all. Guessing in advance won''t help....that way lies madness :-) A. -- mailto:alancfrancis@gmail.com http://blog.alancfrancis.com/