3
鑑於以下車型如何在需要時創建並保存父和子記錄= true?
class Parent
has_many :children
end
class Child
belongs_to :parent, required: true
end
是否有可能在同一時間創建家長和孩子?
@parent = Parent.new(valid_attributes)
@parent.children.build(valid_attributes)
@parent.save
@parent.errors.messages
#=> {:"children.parent"=>["must exist"]}
刪除required: true
允許記錄保存。但是有沒有辦法讓父母和孩子一起保存,同時仍然驗證父母是否存在?
你在正確的軌道,但孩子的正常多的是兒童不要孩子的。這在軌道上很重要,因爲它會弄亂變形。還有幾個語法錯誤。 – max
感謝@max指出這一點,我已經更新了我的答案。請讓我知道我在哪裏使用錯誤的語法? – Satendra
看我的編輯。兩個地方的結腸與兒童之間有一段距離。 – max