# Foreign keys not being saved by nested forms

**URL:** <https://www.ruby-forum.com/t/foreign-keys-not-being-saved-by-nested-forms/178399>\
**Category:** Rails\
**Created:** [November 3, 2009, 11:06pm UTC](https://www.ruby-forum.com/t/foreign-keys-not-being-saved-by-nested-forms/178399 "2009-11-03T23:06:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Todd\_ASJacobs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f17d59/32.png) [@Todd\_ASJacobs](https://www.ruby-forum.com/u/Todd_ASJacobs)\
**Post date:** [November 3, 2009, 11:06pm UTC](https://www.ruby-forum.com/t/foreign-keys-not-being-saved-by-nested-forms/178399/1 "2009-11-03T23:06:41Z")

</div>

I have the following included in my schema:

create\_table “users”, :force =\> true do |t|  
t.string “login”  
t.string “first\_name”  
t.string “last\_name”  
t.string “email”  
t.string “password”  
t.integer “contact\_id”  
t.datetime “created\_at”  
t.datetime “updated\_at”  
end  
create\_table “contacts”, :force =\> true do |t|  
t.string “company\_name”  
t.integer “address\_id”  
t.integer “phone”  
t.string “website”  
t.integer “biztype\_id”  
t.datetime “created\_at”  
t.datetime “updated\_at”  
end  
create\_table “addresses”, :force =\> true do |t|  
t.string “street\_address1”  
t.string “street\_address2”  
t.string “city”  
t.integer “state\_id”  
t.integer “zipcode”  
t.integer “plus4”  
t.datetime “created\_at”  
t.datetime “updated\_at”  
end

In other words, a user has a foreign key linking to the contacts table,  
and a contact is linked to an address. In my models, I have defined:

```
class User < ActiveRecord::Base

```

has\_one :contact  
has\_one :address, :through =\> :contact  
has\_many :properties  
has\_many :notes, :through =\> :properties

accepts\_nested\_attributes\_for :contact, {  
:allow\_destroy =\> true,  
:reject\_if =\> :all\_blank  
}  
accepts\_nested\_attributes\_for :address, {  
:allow\_destroy =\> true,  
:reject\_if =\> :all\_blank  
}  
end  
class Contact \< ActiveRecord::Base  
belongs\_to :user  
has\_one :address  
has\_one :biztype  
end  
class Address \< ActiveRecord::Base  
belongs\_to :contact  
belongs\_to :property  
end

In order to create a nested form, my AccountsController says:

def new  
@user = User.new  
@user.build\_contact  
@user.contact.build\_address  
respond\_to do |format|  
format.html  
format.xml { render :xml =\> @user }  
end  
end

but while the form will accept the data, nothing is populating the  
foreign keys. In other words, I end up with unassociated records in the  
database, when I’m actually expecting the records of the nested form to  
be correctly linked.

What am I doing wrong here?

–  
“Oh, look: rocks!”  
– Doctor Who, “Destiny of the Daleks”

---

<div class="post-metadata">

**Author:** ![Todd\_ASJacobs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f17d59/32.png) [@Todd\_ASJacobs](https://www.ruby-forum.com/u/Todd_ASJacobs)\
**Post date:** [November 3, 2009, 11:07pm UTC](https://www.ruby-forum.com/t/foreign-keys-not-being-saved-by-nested-forms/178399/2 "2009-11-03T23:07:51Z")

</div>

On Nov 3, 9:39 pm, “Todd A. Jacobs” \<tjacobs-sndr-  
removed\_email\_address@domain.invalid\> wrote:

> but while the form will accept the data, nothing is populating the  
> foreign keys. In other words, I end up with unassociated records in the  
> database, when I’m actually expecting the records of the nested form to  
> be correctly linked.

Your associations are back to front - users has a contact\_id column,  
so user belongs\_to contact, not has\_one (ditto contacts and  
address\_id)

Fred

---

<div class="post-metadata">

**Author:** ![Todd\_ASJacobs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f17d59/32.png) [@Todd\_ASJacobs](https://www.ruby-forum.com/u/Todd_ASJacobs)\
**Post date:** [November 4, 2009, 2:24am UTC](https://www.ruby-forum.com/t/foreign-keys-not-being-saved-by-nested-forms/178399/3 "2009-11-04T02:24:32Z")

</div>

On Tue, Nov 03, 2009 at 02:06:32PM -0800, Frederick C. wrote:

> Your associations are back to front - users has a contact\_id column,  
> so user belongs\_to contact, not has\_one (ditto contacts and  
> address\_id)

Thanks, that fixed it. I think my difficulty is that, while has\_many  
generally makes sense semantically, belongs\_to doesn’t always. As long  
as I think of it as “has\_foreign\_key\_for” instead, it all works out  
fine.

For example, in my case a contact has a foreign key to an address. The  
address “belongs to” the contact (not the other way around), but I need  
to use belongs\_to in the model to build the proper relationship.

Thanks again for the help.

–  
“Oh, look: rocks!”  
– Doctor Who, “Destiny of the Daleks”

---

<div class="post-metadata">

**Author:** ![Todd\_ASJacobs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f17d59/32.png) [@Todd\_ASJacobs](https://www.ruby-forum.com/u/Todd_ASJacobs)\
**Post date:** [November 4, 2009, 6:57am UTC](https://www.ruby-forum.com/t/foreign-keys-not-being-saved-by-nested-forms/178399/4 "2009-11-04T06:57:18Z")

</div>

hi,same here,i cant resove this problem.But i found a demo ,that’s  
awesome.

[http://github.com/anathematic/has\_one\_problem](http://github.com/anathematic/has_one_problem)

2009/11/4 Todd A. Jacobs [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> For example, in my case a contact has a foreign key to an address. The  
> address “belongs to” the contact (not the other way around), but I need  
> to use belongs\_to in the model to build the proper relationship.
> 
> Thanks again for the help.

–  
tommy xiao  
E-mail: xiaods(AT)[gmail.com](http://gmail.com)
