Forum: Ruby on Rails Noob ? - Foreign Key

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
1b83995ebdc4e6eac2ee0fca6a2bb439?d=identicon&s=25 Lewis Skolnick (Guest)
on 2008-12-10 09:56
(Received via mailing list)
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
2d8132658d56e51f19ace1c68e48b6aa?d=identicon&s=25 Thorsten Mueller (thorsten)
on 2008-12-10 10:36
(Received via mailing list)
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
This topic is locked and can not be replied to.