【发布时间】:2014-04-01 00:46:29
【问题描述】:
我正在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 => "some.person@domain.com",
:zip => "12345"}).first_or_initialize
contact.build_user if contact.user == nil
contact.save!
【问题讨论】:
标签: ruby-on-rails activerecord ruby-on-rails-3.1 ruby-1.8.7