On 1 September 2012 16:15, Jason Walsh
<lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org>
wrote:> 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
>
> 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 :)
>
> Jase
>
> --
> Posted via http://www.ruby-forum.com/.
>
> --
> You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
> To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
> To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit https://groups.google.com/groups/opt_out.