如何用不同的外键创建相同型号的工厂女孩

时间:2013-12-16 07:42:25

标签: ruby-on-rails cucumber factory-bot

我有一个任务模型,它有很多用户,任务属于用户。我可以将任务分配给其他用户。

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的任务的工厂女孩​​。我已经完成了以下代码

工厂/ 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$/'

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

1 个答案:

答案 0 :(得分:0)

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