You shouldn't have to worry about that.  In this code:

Product.transaction do
  @product.save!               #<-- this saves the product, retrieving the 
db-assigned ID
  @details.product = @product  #<-- this writes @product.id to 
@details.product_id
  @details.save!               #<-- this saves the @details (since it's 
associated w/the product)
  redirect_to :action => :show, :id => @product
end

HTH

-Roy
-----Original Message-----
From: [email protected] [mailto:[EMAIL PROTECTED] On Behalf Of 
gnunix
Sent: Tuesday, October 21, 2008 9:41 AM
To: Ruby on Rails: Talk
Subject: [Rails] Returning a record's ID before the record is made?!??


I am new to Ruby, so apologies.

I'm attempting to combine the input of two objects into one form.  The objects 
are customer and registration.  What concerns me is how will I be able to 
return the record ID of the customer object to the registration object so that 
when the registration object is saved it contains that customer ID?

I am learning via the book Agile Web Development with Rails (Third
Edition) and in the section on Action view it describes in a section called 
"Multiple Models in a Form" a technique used to do this in a form using the 
fields_for method.  Then in the registration controller I would specify the 
creation of a new customer object in the new action.  I would also create in 
the create action of this controller a transaction to ensure that (from the 
book) 1. If either model contains invalid data, neither model should be saved.
2. If both models contain validation errors, we want to display the messages 
from both...

I couldn't agree more.  However in the registration object there is a field 
called customer_id that has to be populated with the record_id from the 
customer model.

How does one get around this?  Thanks for your thoughts in advance.



--~--~---------~--~----~------------~-------~--~----~
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