2016-05-17 3 views
0

Как я могу удалить отношения has_one с фиксацией в базу данных?Rails remove 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 
Смежные вопросы