0
我正在創建聯繫人& DB中的用戶seed.rb
。使用下面的代碼,聯繫人被保存,但子女用戶不是。我錯過了什麼?我嘗試過使用和不使用if
聲明,並且用戶從不保存。在種子期間未保存子模型
模式
class Contact < BaseModel
#...
has_one :user
end
class User < BaseModel
#...
belongs_to :contact
end
種子
contact = Contact.where({
:first_name => "Some",
:last_name => "Person",
:email => "[email protected]",
:zip => "12345"}).first_or_initialize
contact.build_user if contact.user == nil
contact.save!
我不喜歡它,但它的作品。我習慣於C#(LINQ,EF),其中父代被提交給數據庫時,子模型會自動更新其父代的ID。謝謝! –