Indeed, you should save (persist) your object (script) after creating it or use the create (or create!) method instead or new: @script = Script.new(:video_id => self.id, :startp => 0, :text => 'ToDo: ') @scipt.save
or @script = Script.create(:video_id => self.id, :startp => 0, :text => 'ToDo: ') regards Geoffroy On Friday, March 9, 2012 9:58:45 AM UTC+1, Colin Law wrote: > > On 9 March 2012 02:15, Soichi Ishida <li...@ruby-forum.com> 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 view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/39DRa9nuzzgJ. To post to this group, send email to rubyonrails-talk@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.