Factory Girl链接表与外键

时间:2016-08-10 13:11:58

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

想要为相关表格创建相关记录:

class Product < ActiveRecord::Base
has_one :product_cstm, :foreign_key=>'id_c'
end

class ProductCstm < ActiveRecord::Base
self.table_name = 'product_cstm'
self.primary_key = 'id_c'
belongs_to :product, :foreign_key=>'id_c'
end

因此,表格与Products.id和Product_cstm.id_c

相关联

我创建了以下工厂:

FactoryGirl.define do
 factory :product do
   id_c { SecureRandom.uuid }
 end
end

FactoryGirl.define do
 factory :product_cstm do
   accociation :product_cstm
 end
end

是否可以创建工厂(和记录),其中将为product.id和product_cstm.id_c生成相同的UUID?

1 个答案:

答案 0 :(得分:0)

嗯,不确定为什么你有两个同名的工厂。

关联需要在同一个区块内完成。像

这样的东西
FactoryGirl.define do
    factory :product do
        id_c { SecureRandom.uuid }
        association: product_cstm, strategy: :build
    end
end
相关问题