2016-05-17 5 views
0

has_oneの関係をデータベースにコミットするにはどうしたらいいですか?Railsはhas_oneを保存せずに削除します

class Foo 
    has_one :bar 

    validates :bar, presence: true 
end 

class Bar 
    belongs_to :foo 

    validates :foo, presence: true 
end 

foo = Foo.new 
foo.build_bar 
foo.save # => success 

foo.bar = nil # => Failed to remove the existing associated bar. The record failed to save after its foreign key was set to nil. 

私はしばらくオンラインで検索していましたが、良い答えが見つかりません。

答えて

0

あなたがいないbarそれが外部キー

試してみる必要があるとして、nilとして割り当てることができます。

foo.bar.destroy 
関連する問題