我在建立协会时遇到了麻烦。我的模型定义如下:
class Conversation
belongs_to :user1
belongs_to :user2
has_many :messages
end
我已经定义了这些工厂
factory :user do
name "name"
end
factory :female, parent: :user do
gender 'f'
end
factory :male, parent: :user do
gender 'm'
end
factory :message do
message "message"
conversation
end
现在我正在尝试像这样创建工厂“conversation_with_messages”
factory :conversation do
read false
association :user1, factory: :male
association :user2, factory: :female
factory :conversation_with_messages do
ignore do
messages_count 10
end
after(:create) do |conversation, evaluator|
FactoryGirl.create_list(:message, evaluator.messages_count, author: conversation.user1)
end
end
end
但是这样做FactoryGirl.create(:conversation_with_messages)
会导致数据库错误,提示 user1_id 列必须不为空。
我想知道为什么这个专栏没有填满,我在这里做错了什么?