# About a simple insert

**URL:** <https://www.ruby-forum.com/t/about-a-simple-insert/104212>\
**Category:** Rails\
**Created:** [May 15, 2007, 2:05pm UTC](https://www.ruby-forum.com/t/about-a-simple-insert/104212 "2007-05-15T14:05:27Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![turnkeytraders](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ba8739/32.png) [@turnkeytraders](https://www.ruby-forum.com/u/turnkeytraders)\
**Post date:** [May 15, 2007, 2:05pm UTC](https://www.ruby-forum.com/t/about-a-simple-insert/104212/1 "2007-05-15T14:05:27Z")

</div>

Ok, my problem is that I have a form with a client’s information. Now in  
the form, I ask the client for his info, which inserts into the table  
“clients”, and then I have another text input where he has to provide a  
specific detail that i want to be inserted into the table “details”. How  
can I insert this data into 2 different tables?

---

<div class="post-metadata">

**Author:** ![turnkeytraders](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ba8739/32.png) [@turnkeytraders](https://www.ruby-forum.com/u/turnkeytraders)\
**Post date:** [May 15, 2007, 3:10pm UTC](https://www.ruby-forum.com/t/about-a-simple-insert/104212/2 "2007-05-15T15:10:01Z")

</div>

On 5/15/07, Matthew L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Ok, my problem is that I have a form with a client’s information. Now in  
> the form, I ask the client for his info, which inserts into the table  
> “clients”, and then I have another text input where he has to provide a  
> specific detail that i want to be inserted into the table “details”. How  
> can I insert this data into 2 different tables?

It sounds like you should establish an association between the two  
models:

class Client \< ActiveRecord::Base  
has\_many :details  
end

class Detail \< ActiveRecord::Base  
belongs\_to :client  
end

This would require a client\_id column in the details table.

Then, when the form is submitted, you would do something like:

client = Client.new(params[:client])  
client.details.build(params[:detail])  
client.save

The one save inserts both the clients and details rows.

HTH

---

<div class="post-metadata">

**Author:** ![turnkeytraders](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ba8739/32.png) [@turnkeytraders](https://www.ruby-forum.com/u/turnkeytraders)\
**Post date:** [May 15, 2007, 5:16pm UTC](https://www.ruby-forum.com/t/about-a-simple-insert/104212/3 "2007-05-15T17:16:29Z")

</div>

Bob S. wrote:

> On 5/15/07, Matthew L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Ok, my problem is that I have a form with a client’s information. Now in  
> > the form, I ask the client for his info, which inserts into the table  
> > “clients”, and then I have another text input where he has to provide a  
> > specific detail that i want to be inserted into the table “details”. How  
> > can I insert this data into 2 different tables?
> 
> It sounds like you should establish an association between the two  
> models:
> 
> class Client \< ActiveRecord::Base  
> has\_many :details  
> end
> 
> class Detail \< ActiveRecord::Base  
> belongs\_to :client  
> end
> 
> This would require a client\_id column in the details table.
> 
> Then, when the form is submitted, you would do something like:
> 
> client = Client.new(params[:client])  
> client.details.build(params[:detail])  
> client.save
> 
> The one save inserts both the clients and details rows.
> 
> HTH

Ok i got that, but in my new.rhtml, what do I put in the value field of  
my text input? I tried \<%= @client.details.name %\> but it doesn’t work.

Thanks alot for the help

---

<div class="post-metadata">

**Author:** ![turnkeytraders](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ba8739/32.png) [@turnkeytraders](https://www.ruby-forum.com/u/turnkeytraders)\
**Post date:** [May 15, 2007, 5:20pm UTC](https://www.ruby-forum.com/t/about-a-simple-insert/104212/4 "2007-05-15T17:20:05Z")

</div>

Matthew L. wrote:

> Bob S. wrote:  
> On 5/15/07, Matthew L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> Ok, my problem is that I have a form with a client’s information. Now in  
> the form, I ask the client for his info, which inserts into the table  
> “clients”, and then I have another text input where he has to provide a  
> specific detail that i want to be inserted into the table “details”. How  
> can I insert this data into 2 different tables?

It sounds like you should establish an association between the two

> models:
> 
> class Client \< ActiveRecord::Base  
> has\_many :details  
> end
> 
> class Detail \< ActiveRecord::Base  
> belongs\_to :client  
> end
> 
> This would require a client\_id column in the details table.
> 
> Then, when the form is submitted, you would do something like:
> 
> client = Client.new(params[:client])  
> client.details.build(params[:detail])  
> client.save
> 
> The one save inserts both the clients and details rows.
> 
> HTH
> 
> Ok i got that, but in my new.rhtml, what do I put in the value field of  
> my text input? I tried \<%= @client.details.name %\> but it doesn’t work.
> 
> Thanks alot for the help

Oh and what do i put as my id and name for my text input also?

Thanks

---

<div class="post-metadata">

**Author:** ![turnkeytraders](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ba8739/32.png) [@turnkeytraders](https://www.ruby-forum.com/u/turnkeytraders)\
**Post date:** [May 15, 2007, 5:45pm UTC](https://www.ruby-forum.com/t/about-a-simple-insert/104212/5 "2007-05-15T17:45:57Z")

</div>

On 5/15/07, Matthew L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Ok i got that, but in my new.rhtml, what do I put in the value field of  
> my text input? I tried \<%= @client.details.name %\> but it doesn’t work.

Oh, I see what you’re saying.

You should keep two separate objects for the purpose of the form:

(in controller)

@client = Client.new  
@detail = Detail.new

Then in the view:

\<%= text\_field :detail, :name %\>

Before the save, you would add the detail to the client:

@client.details \<\< @detail  
@client.save

---

<div class="post-metadata">

**Author:** ![turnkeytraders](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ba8739/32.png) [@turnkeytraders](https://www.ruby-forum.com/u/turnkeytraders)\
**Post date:** [May 15, 2007, 5:54pm UTC](https://www.ruby-forum.com/t/about-a-simple-insert/104212/6 "2007-05-15T17:54:59Z")

</div>

Bob S. wrote:

> On 5/15/07, Matthew L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:  
> Ok i got that, but in my new.rhtml, what do I put in the value field of  
> my text input? I tried \<%= @client.details.name %\> but it doesn’t work.
> 
> Oh, I see what you’re saying.
> 
> You should keep two separate objects for the purpose of the form:

Wow! Thank you very much! I really needed to know this and i’m glad I  
came here. Thanks alot for your help!
