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?