On 9 March 2012 02:15, Soichi Ishida <[email protected]> wrote:
> Thanks for you answer.
>
>> You have to first save the @video before accessing the id, that is when
>> the
>> after_create or after_save is triggered.
>> In your Video class, the after save has access to the @video instance
>> through the self keyword (self.id in your case).
>>
>
> I need to clarify this point.
>
> When videos_controller.rb calls for 'create' action, a new instance gets
> persisted, correct?
> When doing so, 'after_create' is called as well.  It seems to me that
> when 'after_create' is called, the new video instance is already
> created.  Please correct me, if I'm wrong.

Calling the create action in the controller does not create it in the
database, it is the call of 'save' within the create action that saves
it to the database.  The after_save callback will be called from
within save, after it is saved to the db and then the id will be
valid.

>
> I tried this. but no good ( I knew it! )
>
>  after_create :create_first_script
>
>  def create_first_script
>    @script = Script.new(:video_id => self.id, :startp => 0, :text =>
> 'ToDo: ')
>  end

What do you think that code does?  It creates a new object in memory,
but you have not saved it to the database, so after the method is
called your new object is immediately lost.

Colin

-- 
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 [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to