我有麻烦建立协会。我有我的模型定义如下:如何建立工厂女孩协会?
class Conversation
belongs_to :user1
belongs_to :user2
has_many :messages
end
和我现在我试图像创建工厂“conversation_with_messages”定义这些工厂
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
这
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列需要不为空。
我想知道为什么这个专栏没有填写,我在这里做错了什么?
因为某些原因我需要分组对象,将聚集的消息,并测试本身并不难 - 我只是与我的想法是很简单的例子创建正确FactoryGirl代码中的问题。 – 2013-03-14 15:08:25