Christopher.Preston@csiro.au
2006-Feb-20 12:03 UTC
[Rails] Obtaining the ID of a newly saved model object
Hi, I''m struggling with this and I''m not sure if it is obvious or dumb (or both). I have a basic create method like so: def creatematerial @material = Material.new(params[:material]) if @material.save flash[:notice] = "Material successfully created." end Ok so far, but now I want to retrieve that object that was just saved so that it can be used in the view. Something like... @newmaterial = Material.find(@material.id) is what I was thinking, but that doesn''t work as @material still has no id. The method finishes with a rendering of a table (this is an AJAX called method) render_partial ''listtable'', :layout => false end I want to grab the newly created object so I can hightlight and display it in the view, but I don''t know how to retrieve it again! Any and all ideas are most appreciated. Chris.
Frederick Ros
2006-Feb-20 12:18 UTC
[Rails] Obtaining the ID of a newly saved model object
Quoting Christopher.Preston@csiro.au:> > @newmaterial = Material.find(@material.id) > > is what I was thinking, but that doesn''t work as @material still has no id.Doesn''t it work if you do a @material.reload ? Frederick Ros aka Sleeper -- sleeper@jabber.fr
Are you sure your object has been correctly saved in your database ? I think the save() method should populate the id instance variable. Thomas. -- Posted via http://www.ruby-forum.com/.
Kevin Skoglund
2006-Feb-20 14:41 UTC
[Rails] Re: Obtaining the ID of a newly saved model object
Chris, Rails saves your Model instance and adds the resulting id to its attributes. And you still "have" the object so there is no need to refind it. Something like this should work: def creatematerial @material = Material.new(params[:material]) if @material.save flash[:notice] = "Material successfully created." render(:action => ''showmaterial'', :id => @material) else redirect_to(:action => ''newmaterial'') end end If not, make sure Material is in fact being saved successfully. HTH, Kevin Skoglund> Hi, > > I''m struggling with this and I''m not sure if it is obvious or dumb > (or both). > > I have a basic create method like so: > > def creatematerial > @material = Material.new(params[:material]) > if @material.save > flash[:notice] = "Material successfully created." > end > > Ok so far, but now I want to retrieve that object that was just > saved so that it can be used in the view. Something like... > > @newmaterial = Material.find(@material.id) > > is what I was thinking, but that doesn''t work as @material still > has no id. > > The method finishes with a rendering of a table (this is an AJAX > called method) > > render_partial ''listtable'', :layout => false > end > > I want to grab the newly created object so I can hightlight and > display it in the view, but I don''t know how to retrieve it again! > > Any and all ideas are most appreciated. > > Chris.
Christopher.Preston@csiro.au
2006-Feb-22 22:32 UTC
[Rails] Obtaining the ID of a newly saved model object
> Rails saves your Model instance and adds the resulting id to its > attributes. And you still "have" the object so there is no need to > refind it. > HTH, > Kevin SkoglundThanks, I should have figured that out myself! Chris.