# 1 Form \> 2 Models \> 2nd Model Requres 1st Model ID

**URL:** <https://www.ruby-forum.com/t/1-form-2-models-2nd-model-requres-1st-model-id/174535>\
**Category:** Rails\
**Created:** [August 28, 2009, 9:53am UTC](https://www.ruby-forum.com/t/1-form-2-models-2nd-model-requres-1st-model-id/174535 "2009-08-28T09:53:28Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![brianp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/94ad74/32.png) [@brianp](https://www.ruby-forum.com/u/brianp)\
**Post date:** [August 28, 2009, 9:53am UTC](https://www.ruby-forum.com/t/1-form-2-models-2nd-model-requres-1st-model-id/174535/1 "2009-08-28T09:53:28Z")

</div>

Hey Everyone,

So I have one form that submits information to create 2 models (A, B).  
I need the ID of model A to be inserted into model B. But the ID isn’t  
created until it hits the database (auto\_increment). But if for some  
reason either item cannot be created or saved I want to abort the  
whole operation.

Whats the best method to do this ?

thanks.

---

<div class="post-metadata">

**Author:** ![brianp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/94ad74/32.png) [@brianp](https://www.ruby-forum.com/u/brianp)\
**Post date:** [August 28, 2009, 6:43pm UTC](https://www.ruby-forum.com/t/1-form-2-models-2nd-model-requres-1st-model-id/174535/2 "2009-08-28T18:43:24Z")

</div>

google “rails 1 form 2 models”

---

<div class="post-metadata">

**Author:** ![brianp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/94ad74/32.png) [@brianp](https://www.ruby-forum.com/u/brianp)\
**Post date:** [August 29, 2009, 1:28am UTC](https://www.ruby-forum.com/t/1-form-2-models-2nd-model-requres-1st-model-id/174535/3 "2009-08-29T01:28:23Z")

</div>

I can handle building two models from 1 form. It’s getting the id from  
the first saved model and inserting it in the second and rolling back  
the whole transaction if anything goes wrong that is the problem.

---

<div class="post-metadata">

**Author:** ![brianp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/94ad74/32.png) [@brianp](https://www.ruby-forum.com/u/brianp)\
**Post date:** [August 29, 2009, 1:16pm UTC](https://www.ruby-forum.com/t/1-form-2-models-2nd-model-requres-1st-model-id/174535/4 "2009-08-29T13:16:11Z")

</div>

Hi brianp  
From what I understood you have to put the whole thing in a  
transaction like

ActiveRecord::Base.transaction do  
save first model object  
save second model object  
end

Sijo

---

<div class="post-metadata">

**Author:** ![brianp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/94ad74/32.png) [@brianp](https://www.ruby-forum.com/u/brianp)\
**Post date:** [August 31, 2009, 8:40am UTC](https://www.ruby-forum.com/t/1-form-2-models-2nd-model-requres-1st-model-id/174535/5 "2009-08-31T08:40:11Z")

</div>

Thanks everyone for your suggestions.

This is what I came up with I was looking for some feedback. It works  
fine but I figure I might be doing something long winded. This is my  
first go at an actual railsApp other then book demos. Any feedback  
would be nice!

// new.html.erb

# New word

\<%= error\_messages\_for :word, :definition %\>

\<% form\_for(@word) do |f| %\>

\<%= f.label :word %\>  
 \<%= f.text\_field :word %\>

\<%= f.label :was\_created\_by %\>  
 \<%= f.check\_box :was\_created\_by %\>

\<% fields\_for(:definition, @definition) do |i| %\>

\<%= i.label :definition %\>  
  
\<%= i.text\_area :definition %\>

  

\<%= i.label :word\_type %\>  
  
\<%= i.text\_field :word\_type %\>

  

\<%= i.label :pronounciation %\>  
  
\<%= i.text\_field :pronounciation %\>

  

\<%= i.label :origin %\>  
  
\<%= i.text\_area :origin %\>

  

\<%= i.label :is\_profain %\>  
  
\<%= i.check\_box :is\_profain %\>

  
\<% end %\> 

\<%= submit\_tag 'Create' %\>

\<% end %\>

\<%= link\_to ‘Back’, words\_path %\>

// words\_controller.rb  
def create

```
@client_ip = request.remote_ip

begin
  ActiveRecord::Base.transaction do
    @word = Word.new(params[:word].merge(:ip_address =>

```

@client\_ip, :user\_id =\> ‘1’))  
@word.save  
@definition = Definition.new(params[:definition].merge  
(:ip\_address =\> @client\_ip,  
:user\_id  
=\> ‘1’,  
:word\_id  
=\> @word.id,  
:status  
=\> ‘published’))  
@definition.save  
end  
rescue ActiveRecord::RecordInvalid =\> invalid  
flash[:notice] = ‘Word was not created’  
render :action =\> “new”  
end

```
redirect_to(@word)

```

end
