Has_many - when are child objects created?

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 B. wrote:

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