# Noob ? - Foreign Key

**URL:** <https://www.ruby-forum.com/t/noob-foreign-key/155293>\
**Category:** Rails\
**Created:** [December 10, 2008, 9:56am UTC](https://www.ruby-forum.com/t/noob-foreign-key/155293 "2008-12-10T09:56:57Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lewis\_Skolnick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/c57346/32.png) [@Lewis\_Skolnick](https://www.ruby-forum.com/u/Lewis_Skolnick)\
**Post date:** [December 10, 2008, 9:56am UTC](https://www.ruby-forum.com/t/noob-foreign-key/155293/1 "2008-12-10T09:56:57Z")

</div>

I am attempting to use scaffolding to create a simple foreign key  
example. But I am not quite understanding how rails writes data into  
the foreign table. When the “belongs\_to” and “has\_many” lines are  
added into model, will rails automatically add the information into  
the foreign table? Or do I have to modify the controller to insert  
the data into the foreign table? Could someone give me an example. I  
would like the data in the foreign table not to repeat itself.

The sample tables I am attempting to work with are Parts and  
Revisions. Where each revision has\_many parts and each part  
belongs\_to revision.

Thanks for any Help!!!

Lewis

---

<div class="post-metadata">

**Author:** ![Lewis\_Skolnick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/c57346/32.png) [@Lewis\_Skolnick](https://www.ruby-forum.com/u/Lewis_Skolnick)\
**Post date:** [December 10, 2008, 10:36am UTC](https://www.ruby-forum.com/t/noob-foreign-key/155293/2 "2008-12-10T10:36:12Z")

</div>

ok, so if parts belongs\_to Revision, it must have a revision\_id  
which you must have created in the migration.

Assuming you have a form that sends the Parts data (in params[:parts]  
and  
you know to which revision it should be added, then

@revision = Revision.find(revision\_id) # however you get revision id,  
could eg be params[:revision\_id]  
@revision.parts.create(params[:parts])

would be all you need.  
First line getting the revision, next line adding the part to that  
revision.  
revision\_id is set by Rails
