Foreign keys not being saved by nested forms

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?

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

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.

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

http://github.com/anathematic/has_one_problem