gpt4 book ai didi

ruby-on-rails - 如何处理工厂女孩中的外键关联

转载 作者:行者123 更新时间:2023-12-05 05:24:18 24 4
gpt4 key购买 nike

我有两个模型通过它们共享的 foreign_key 属性连接。

# app/models/building.rb
belongs_to :building_basic_info, primary_key: "name", foreign_key:"name"

# app/models/building_basic_info.rb
has_many :buildings, primary_key: "name", foreign_key:"name"

我正在尝试编写一个将创建 buildingbuilding_basic_info 并通过属性 name 关联它们的工厂。

FactoryGirl.define do 
factory :building do
name "cannon"
factory :building_with_bbi do
name "cannon"
end
end
end

building = build_stubbed(:building)
building.building_basic_info
# => nil... but I want it to return the associated building_basic_info object

非常感谢任何帮助。

最佳答案

FactoryGirl.define do 
factory :building do
name "cannon"
association :building_basic_info
end

factory :building_basic_info do
name "cannon"
end
end

关于ruby-on-rails - 如何处理工厂女孩中的外键关联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35122833/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com