Creating or updating a has_one ActiveRecord association

if property.contract.nil?
  property.create_contract(some_attributes)
else
  property.contract.update_attributes(some_attributes)
end

Should do the trick. When you have a has_one or belongs_to association then you get build_foo and create_foo methods (which are like Foo.new and Foo.create). If the association already exists then property.contract is basically just a normal active record object.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)