1
我有用戶和教師模型。教師belongs_to
用戶和用戶has_one
老師。我也有在工廠女孩文件中的代碼:Rails 3 - 工廠女孩寶石 - belongs_to和has_one的關係
Factory.define :user do |user|
user.user_login "Another User"
user.user_role "admin"
user.password "foobar"
end
Factory.sequence :user_login do |n|
"person-#{n}"
end
Factory.define :teacher do |teacher|
teacher.teacher_last_name 'Last'
teacher.teacher_first_name 'First'
teacher.teacher_middle_name 'Middle'
teacher.teacher_birthday '01.11.1980'
teacher.teacher_category 'First category'
teacher.teacher_sex 'm'
end
當我嘗試創建我的規格老師:
@teacher = Factory(:teacher)
然後我收到錯誤:
Failure/Error: @teacher = Factory(:teacher)
ActiveRecord::RecordInvalid:
Validation failed: User can't be blank
正如我明白這一點,因爲我不告訴工廠,我的老師belongs_to
用戶。我該如何解決這個問題?
同意你不需要在'user_'和'teacher_'前添加列名。 – 2012-09-14 18:04:41