Construction methods

Hi

Is there a method to create child objects after a parent object has been
created?

eg i have a process class, which when created should contain at least 1
digram class.

I have looked at the initialize method but not sure when this is invoked
ie when an object is created (ie saved to db) or everytime an object is
instantiated (ie read from db).

I only want to invoke the method to create child object the first time
parent object is saved to the db.

I suppose this is like a transaction (ie both records need to be saved
or none), but again not sure how this is implemented in rails.

Thanks in advance :slight_smile:

Jase

Hi

Is there a method to create child objects after a parent object has been
created?

eg i have a process class, which when created should contain at least 1
digram class.

I have looked at the initialize method but not sure when this is invoked
ie when an object is created (ie saved to db) or everytime an object is
instantiated (ie read from db).

I only want to invoke the method to create child object the first time
parent object is saved to the db.

You could put it in the controller method that creates the parent
object, or you could use after_create in the parent model. You have
not told us the relationship between parent and child - I guess it is
parent has_many children, child belongs_to parent. In which case to
create a child object you can use @parent.children.build( , ) or
.children.create( … ) to create the child. Have a look at the Rails
Guide on associations to see how these work.

Colin