I have a Person model and an Address Model:
class Person < ActiveRecord::Base
has_one :address
accepts_nested_attributes_for :address
end
class Addr
Why is your address built in your new action, and not in the create action? You're building an address from a non saved model, without an id, so the foreign key can't be set. You should keep your @person in your new action, but put your build_address in your create action, after @person has been saved.
You need to have accepts_nested_attributes_for :address
on your Person
model for this to work nicely. In your create
action you can then do this:
def create
@person = Person.new(params[:person])
...
end
Then Rails will take care of the rest.
UPDATE: if the address_id
column is in the people
table then it should be belongs_to :address
, not has_one :address