与同一个父母有多个关联的工厂女孩

Jos*_*ald 5 activerecord ruby-on-rails factory-bot

如何创建具有多个依赖于同一个父级的关联的工厂?

父模型:

class Parent < ActiveRecord::Base
  has_many :codes
  has_many :parent_filters

  validates :parent, :presence => true, :uniqueness => true
end
Run Code Online (Sandbox Code Playgroud)

菲特勒模型:

class Filter < ActiveRecord::base
  has_many :parent_filters
  validates :filter, :presence => true, :uniqueness => true
end
Run Code Online (Sandbox Code Playgroud)

ParentFilter 连接模型:

class ParentFilter < ActiveRecord
  belongs_to :parent
  belongs_to :filter

  validates :filter, :presence => true
  validates :parent, :filter, :presence => true, :uniqueness => [ :scope => filter ]
end
Run Code Online (Sandbox Code Playgroud)

AdhocAttribute 模型:

class AdhocAttribute < ActiveRecord::Base
  has_many :adhoc_mappings

  has_many :codes, :through => :adhoc_mappings
  has_many :parent_filters, :through => adhoc_mappings

  validates :adhoc_attribute, :presence => true, :uniqueness => true
end
Run Code Online (Sandbox Code Playgroud)

代码模型:

class Code < ActiveRecord::Base
  belongs_to :parent

  has_many :adhoc_mappings
  has_many :adhoc_attributes, :through => :adhoc_mappings

  validates :code, :parent, presence: true
  validates :code, uniqueness: {case_sensitive: false}
end
Run Code Online (Sandbox Code Playgroud)

最后但并非最不重要的是,一个特别的映射模型。此模型允许为每个代码为每个 ParentFilter 分配一个临时属性,这是我试图创建的工厂。这个工厂应该要求 ParentFilter 和 Code 的 Parent 是相同的(一旦我得到一个功能工厂,我将添加一个自定义验证来强制执行)。

class AdHocMapping < ActiveRecord::Base

  belongs_to :code
  belongs_to :parent_filter
  belongs_to :adhoc_attribute

  has_one :company, :through => :parent_filter

  validates :code, :parent_filter, presence: true
  validates :code, :uniqueness => { :scope => :parent_filter }
end
Run Code Online (Sandbox Code Playgroud)

如果我要使用直接的 ActiveRecord 创建一个 AdhocMapping,我会像这样构建它:

p = Parent.where(:parent => "Papa").first_or_create
aa = AdhocAttribute(:adhoc_attribute => "Doodle").first_or_create
f = Filter.where(:filter => "Z..W").first_or_create
c = Code.where(:code => "ZYXW", :parent => p).first_or_create
pf = ParentFilter.where(:parent => p, :filter => f).first_or_create
am = AdhocMapping(:adhoc_attribute => aa, :parent_filter => pf, :code => :c).first_or_create
Run Code Online (Sandbox Code Playgroud)

以便分配给 AdhocMapping 的 Code 和 ParentFilter 具有相同的 Parent。我尝试了许多不同的方法来尝试让它在 FactoryGirl 中工作,但我似乎无法让它创建对象。

这是我认为最接近我想要的工厂:

require 'faker'

FactoryGirl.define do
  factory :adhoc_mapping do |f|
    transient do
     p Parent.where(:parent => Faker::Lorem.word).first_or_create
    end

    association :parent_filter, :parent => p
    association :code, :parent => p
    association :adhoc_attribute
  end
end
Run Code Online (Sandbox Code Playgroud)

它给出了一个错误 Trait not registered: p

Mar*_*kus 5

对于我通常after(:build)在 中使用的关联FactoryGirl,我可以准确地说出应该发生什么。association xxx经常没有像我想要的那样工作,我很惭愧。)

所以在你的情况下,我认为你可以解决这个问题:

require 'faker'

FactoryGirl.define do
  factory :adhoc_mapping do |f|
    transient do
      default_parent { Parent.where(parent: Faker::Lorem.word).first_or_create }

      parent_filter {  FactoryGirl.build(:parent_filter, parent: default_parent) }
      code { FactoryGild.build(:code, parent: default_parent) }
      adhoc_attribute { FactoryGirl.build(:adhoc_attribute) }
    end

    after(:build) do |adhoc_mapping, evaluator|
      adhoc_mapping.parent_filter = evaluator.parent_filter
      adhoc_mapping.code = evaluator.code
      adhoc_mapping.adhoc_attribute = evaluator.adhoc_attribute
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

通过使用after(:build)它可以与FactoryGirl.create或一起使用FactoryGirl.build。此外transient default_parent,您甚至可以在构建adhoc_mapping. 现在编辑后,您还可以传递nil给属性,它不会被覆盖。