You can use either an after_create on the model or an observer which observes a Video creation.
Personally, I usually prefer observers which seem less intrusive to me, giving a better to make it active or unactive, but some people think the other way.
To make your first script undestroyable, you can add a column to the model or a ‘before_destroy’ which checks if its the first Script created for a given Video, a method to check this same condition and hide ‘destroy’ links… there is a lot of ways to do this.
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.
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.
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.