Hi All, Sorry if this has been covered before, but I couldn''t find a previous thread on the list that answered my question. Basically I''m creating a new record in a table like so: def create @myInstance = MyModel.new(params[:formVars]) @myInstance.save! end My question is: How do I then get the id of the newly created record within this create method? It seems like it should be pretty straightforward, but I''m having a brainfreeze about it, and I didn''t see a method in the rails api that did that. Thanks in advance for any tips. -- Posted via http://www.ruby-forum.com/.
On 7/10/06, Dan <dans@bias-inc.com> wrote:> Sorry if this has been covered before, but I couldn''t find a previous > thread on the list that answered my question. Basically I''m creating a > new record in a table like so: > > def create > > @myInstance = MyModel.new(params[:formVars]) > @myInstance.save! > > end > > > My question is: How do I then get the id of the newly created record > within this create method? It seems like it should be pretty > straightforward, but I''m having a brainfreeze about it, and I didn''t see > a method in the rails api that did that.@myInstance.id Is that not working for you, or am I missing something obvious about your question? -- James
James Ludlow wrote:> On 7/10/06, Dan <dans@bias-inc.com> wrote: >> Sorry if this has been covered before, but I couldn''t find a previous >> thread on the list that answered my question. Basically I''m creating a >> new record in a table like so: >> >> def create >> >> @myInstance = MyModel.new(params[:formVars]) >> @myInstance.save! >> >> end >> >> >> My question is: How do I then get the id of the newly created record >> within this create method? It seems like it should be pretty >> straightforward, but I''m having a brainfreeze about it, and I didn''t see >> a method in the rails api that did that. > > @myInstance.id > > Is that not working for you, or am I missing something obvious about > your question? >I''ll take a wild guess that Dan was trying to access @myInstance.id before the save. I don''t think @myInstance would get an id though until it''s saved.