# Passing foreign key when creating new child record

**URL:** <https://www.ruby-forum.com/t/passing-foreign-key-when-creating-new-child-record/114686>\
**Category:** Rails\
**Created:** [September 17, 2007, 1:24pm UTC](https://www.ruby-forum.com/t/passing-foreign-key-when-creating-new-child-record/114686 "2007-09-17T13:24:11Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dacbo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b9e5f3/32.png) [@dacbo](https://www.ruby-forum.com/u/dacbo)\
**Post date:** [September 17, 2007, 1:24pm UTC](https://www.ruby-forum.com/t/passing-foreign-key-when-creating-new-child-record/114686/1 "2007-09-17T13:24:11Z")

</div>

What would be considered the “standard” way of setting the foreign key  
on a child record when starting from a view of the parent record? E.g.  
you’re on a view to a person record and use a link\_to load a form that  
will enable you to add an address for that person (addresses are  
stored in a separate table):

\<%= link\_to ‘New address’, { :controller =\> ‘addresses’, :action =\>  
‘new’ } %\>

The create action in the addresses controller needs to set the  
person\_id.

Thanks for your input.

---

<div class="post-metadata">

**Author:** ![dacbo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b9e5f3/32.png) [@dacbo](https://www.ruby-forum.com/u/dacbo)\
**Post date:** [September 25, 2007, 11:02pm UTC](https://www.ruby-forum.com/t/passing-foreign-key-when-creating-new-child-record/114686/2 "2007-09-25T23:02:44Z")

</div>

One way is to add person\_id to the link to the address construction  
form:

\<%= link\_to ‘New address’, { :controller =\> ‘addresses’, :action  
=\>‘new’, :person\_id =\> person } %\>

Then pass the person\_id to the address construction view and store it in  
a hidden field

then in the create method:  
@person = Person.find(params[:person\_id])  
@person.create\_address(params[:address])

---

<div class="post-metadata">

**Author:** ![dacbo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b9e5f3/32.png) [@dacbo](https://www.ruby-forum.com/u/dacbo)\
**Post date:** [September 25, 2007, 11:04pm UTC](https://www.ruby-forum.com/t/passing-foreign-key-when-creating-new-child-record/114686/3 "2007-09-25T23:04:16Z")

</div>

Thanks for the pointer. I’d looked around and couldn’t find a clear  
example. Here’s the relevant code in the various files I used to get  
this working.

views/people/show.rhtml

\<%= link\_to ‘New address’, { :controller =\> ‘addresses’, :action =\>  
‘new’, :person\_id =\> @person } %\>

views/addresses/new.rhtml

\<% form\_for :address, :url =\> { :action =\> :create } do |form| %\>

\<%= form.hidden\_field :person\_id, :value =\> params[:person\_id] %\>

[… other form fields, save button, etc.]  
\<% end %\>

models/person.rb

class Person \< ActiveRecord::Base  
has\_many :addresses  
end

models/address.rb

class Address \< ActiveRecord::Base  
belongs\_to :person

# So the addresses table needs a ‘person\_id’ column, which is the

foreign key  
end

controllers/addresses\_controller.rb  
def new  
@address = Address.new  
end  
def create  
@address = Address.new(params[:address])  
[…]  
end
