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.

In their example,

def create
@product = Product.new(params[:product])
@details = Detail.new(params[:details])
Product.transaction do
@product.save!
@details.product = @product
@details.save!
redirect_to :action => :show, :id => @product
end
rescue ActiveRecord::RecordInvalid => e
@details.valid? # force checking of errors even if products failed
render :action => :new
end

they assign the product to the details, @details.product = @product. When you do that with related ActiveRecord objects, ActiveRecord takes care of setting the IDs for you. In this case, @details.product_id will be set to @product.id.

Regards,
Craig

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

Awesome! Thanks a lot!