# Returning a record's ID before the record is made?!?

**URL:** <https://www.ruby-forum.com/t/returning-a-records-id-before-the-record-is-made/151498>\
**Category:** Rails\
**Created:** [October 22, 2008, 7:22am UTC](https://www.ruby-forum.com/t/returning-a-records-id-before-the-record-is-made/151498 "2008-10-22T07:22:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mrkohrea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4491bb/32.png) [@mrkohrea](https://www.ruby-forum.com/u/mrkohrea)\
**Post date:** [October 22, 2008, 7:22am UTC](https://www.ruby-forum.com/t/returning-a-records-id-before-the-record-is-made/151498/1 "2008-10-22T07:22:52Z")

</div>

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 D. 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.

---

<div class="post-metadata">

**Author:** ![mrkohrea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4491bb/32.png) [@mrkohrea](https://www.ruby-forum.com/u/mrkohrea)\
**Post date:** [October 22, 2008, 3:35pm UTC](https://www.ruby-forum.com/t/returning-a-records-id-before-the-record-is-made/151498/2 "2008-10-22T15:35:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mrkohrea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4491bb/32.png) [@mrkohrea](https://www.ruby-forum.com/u/mrkohrea)\
**Post date:** [October 23, 2008, 12:37am UTC](https://www.ruby-forum.com/t/returning-a-records-id-before-the-record-is-made/151498/3 "2008-10-23T00:37:51Z")

</div>

Craig D. wrote:

> In their example,  
> … [snip]  
> 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

Thanks for your post Craig. I could not wrap my head around this  
concept, partially because it was so difficult for me to implement in  
JSP that I resorted to using randomly generated UUID’s before inputing  
the record into the database.

Active Record to the rescue indeed. Wow. I spent about an hour playing  
with this last night and was able to magically get this to, well, just  
work in Rails.

When I wrote this post originally I hadn’t yet tried the example as I  
just couldn’t understand the code and how it worked. I took the leap of  
faith and followed the example and Ta-da, onto more interesting  
problems.

I should also not that another user wrote me off-list and I’d like to  
thank him/her for the wonderfully helpful response. I am not mentioning  
his/her name in the event that they wrote off list because they wanted  
to remain anonymous. But thank you. They suggested I read:  
[http://railsguides.phusion.nl/activerecord/association\_basics.html](http://railsguides.phusion.nl/activerecord/association_basics.html)  
and it helped immensely. I felt like I missed a meeting!

Ruby/Rails has been such a fun experiment thus far. I’m really loving  
it’s elegance and simplicity. Thanks again to all. TTFN.

---

<div class="post-metadata">

**Author:** ![mrkohrea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4491bb/32.png) [@mrkohrea](https://www.ruby-forum.com/u/mrkohrea)\
**Post date:** [October 23, 2008, 2:28pm UTC](https://www.ruby-forum.com/t/returning-a-records-id-before-the-record-is-made/151498/4 "2008-10-23T14:28:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mrkohrea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4491bb/32.png) [@mrkohrea](https://www.ruby-forum.com/u/mrkohrea)\
**Post date:** [December 10, 2008, 11:13am UTC](https://www.ruby-forum.com/t/returning-a-records-id-before-the-record-is-made/151498/5 "2008-12-10T11:13:48Z")

</div>

On Oct 23, 5:26 pm, “Pardee, Roy” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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

Awesome! Thanks a lot!
