ruby-on-rails - 与同一个 parent 有多个关联的工厂女孩

标签 ruby-on-rails activerecord factory-bot

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

父模型:

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

  validates :parent, :presence => true, :uniqueness => true
end

菲特勒模型:
class Filter < ActiveRecord::base
  has_many :parent_filters
  validates :filter, :presence => true, :uniqueness => true
end

ParentFilter 连接模型:
class ParentFilter < ActiveRecord
  belongs_to :parent
  belongs_to :filter

  validates :filter, :presence => true
  validates :parent, :filter, :presence => true, :uniqueness => [ :scope => filter ]
end

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

代码模型:
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

最后但并非最不重要的是,一个特别的映射模型。此模型允许为每个代码为每个 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

如果我要使用直接的 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

以便分配给 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

它给出了 Trait not registered: p 的错误

最佳答案

对于协会,我通常使用 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

通过使用 after(:build)它适用于 FactoryGirl.createFactoryGirl.build .另外还有transient default_parent ,您甚至可以在构建 adhoc_mapping 时显式设置您想要的父级。 .现在在 之后编辑 您也可以通过nil属性,它不会被覆盖。

关于ruby-on-rails - 与同一个 parent 有多个关联的工厂女孩,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33833104/

相关文章:

ruby-on-rails - rspec-mocks 的 double 被设计为只持续一个例子

ruby-on-rails - rails ActiveRecord : Saving nested models is rolled back

ruby-on-rails - 在rails中,为模型创建factorygirl时,不应该调用模型的before_create吗?

ruby-on-rails - FactoryGirl 序列破坏了我的 rspec 测试(我认为)

php - 他们是怎么做到的呢?动态加载页面/网站......框架,脚本? PHP、jQuery、 ruby ?

mysql - 第 15 行第 2 列语法错误 : ` socket:/tmp/mysql.sock'

javascript - Rails json View 转为 javascript 函数

ruby-on-rails - 如何确定完成 Rails 查询所需的时间

ruby-on-rails - Active Record Where Not bool 值 : true

ruby-on-rails - RSpec stub 不适用于集成测试