2013-12-16 32 views
0

我有一个任务模型,其中有许多用户和任务属于用户。我可以将任务分配给其他用户。如何使用不同的外键为同一模型创建工厂女孩

user.rb

has_many :tasks, dependent: :destroy 
has_many :tasks_assigned, foreign_key: "assigned_to_id", dependent: :destroy 

task.rb

belongs_to :assigned_to , class_name: "User" 
belongs_to :user 

我想创建带有字段任务名称和assigned_to_id任务factorygirl。我也做了下面的代码

工厂/ task.rb

FactoryGirl.define do 
factory :task_wop do |t|  
    t.task "test_task" 
    t.assosciation :assigned_to 
    t.assosciation :user 
    t.association :project 
end 
end 

我在黄瓜用户步骤,使用这个作为

Given /^I have a task without project$/ do 
    @task_wop = FactoryGirl.create(:task_wop, project_id: nil) 
end 

但我得到的错误作为

Attribute already defined: assosciation (FactoryGirl::AttributeDefinitionError) 
    ./features/step_definitions/user_steps.rb:19:in `/^I have a task without project$/' 

我需要帮助来确定错误是什么?为什么会发生。这是定义工厂的正确方法吗?

回答

0

您在工厂中以相同方式(assosciation)拼写association两次,所以它认为您试图定义两次相同的属性。