# How to insert data into a relational database?

**URL:** <https://www.ruby-forum.com/t/how-to-insert-data-into-a-relational-database/69909>\
**Category:** Rails\
**Created:** [September 25, 2006, 3:04pm UTC](https://www.ruby-forum.com/t/how-to-insert-data-into-a-relational-database/69909 "2006-09-25T15:04:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![P.V.Anthony](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/59ef9b/32.png) [@P.V.Anthony](https://www.ruby-forum.com/u/P.V.Anthony)\
**Post date:** [September 25, 2006, 3:04pm UTC](https://www.ruby-forum.com/t/how-to-insert-data-into-a-relational-database/69909/1 "2006-09-25T15:04:18Z")

</div>

Hi.

Please do bear with me as I am a newbie.

I think I understand how to setup the relation using “has\_many” with  
“through”.

What I need to know is how to insert data into the tables in the  
controller.

For example, I have company details like name, address and country.

I also have a category table like audio, video, events and lighting.

Once I setup the relations, I need to know how to enter the data into  
the tables.

Since there are two tables and I am using the “has\_many” do I have to  
write two statements. One statement to insert into table one and another  
one to write to table two.

Is there a way to insert data like how it is done for  
“has\_many\_belong\_to\_many” where we can just use “\<\<”?

I have also bought the pdf version of the agile book for rails version  
2. If there is a page I should look at, please do advise.

Or if there is a website, please do send the link.

I really am new at this. Please do not get upset with all these  
questions.

P.V.Anthony

---

<div class="post-metadata">

**Author:** ![P.V.Anthony](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/59ef9b/32.png) [@P.V.Anthony](https://www.ruby-forum.com/u/P.V.Anthony)\
**Post date:** [September 25, 2006, 3:22pm UTC](https://www.ruby-forum.com/t/how-to-insert-data-into-a-relational-database/69909/2 "2006-09-25T15:22:20Z")

</div>

On 9/25/06, P.V.Anthony [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> controller.  
> one to write to table two.  
> There are many differnt ways to specify a new record.  
> An example might be

@parent\_thing.create( ChildThing.new )

But there are many more listed in great detail in the docs. As long as  
you’ve run your migration (and setup your db with the migrations) then  
rails  
should look after it.

Is there a way to insert data like how it is done for

> “has\_many\_belong\_to\_many” where we can just use “\<\<”?
> 
> I have also bought the pdf version of the agile book for rails version  
> 2. If there is a page I should look at, please do advise.

has\_many starts on page 338. This is a very good look at how it works.

The depot application is a very good starting applicaiton also.

Or if there is a website, please do send the link.

> I really am new at this. Please do not get upset with all these questions.

Welcome aboard

P.V.Anthony

---

<div class="post-metadata">

**Author:** ![P.V.Anthony](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/59ef9b/32.png) [@P.V.Anthony](https://www.ruby-forum.com/u/P.V.Anthony)\
**Post date:** [September 25, 2006, 3:34pm UTC](https://www.ruby-forum.com/t/how-to-insert-data-into-a-relational-database/69909/3 "2006-09-25T15:34:28Z")

</div>

On this day, 25-September-2006 9:14 PM, Daniel N wrote:

> Welcome aboard  
> Thank you very much for the information. Especially the page number to  
> the agile rails book.

Once again thank you.

P.V.Anthony
