ruby-on-rails Factory Bot:将相同对象传递给关联

avkwfej4  于 4个月前  发布在  Ruby
关注(0)|答案(2)|浏览(51)

模型有2个关联,每个关联属于同一个“组”,如何将同一个组传递给每个关联?

factory :group_feature_config do
  transient do
    group
  end
  association :group_feature, factory: :group_feature, group: group
  association :group_user, factory: :group_user, group: group
end

字符串

iecba09b

iecba09b1#

factory :group_feature_config do
  transient do
    group
  end
  group_feature { association :group_feature, group_id: group.id }
  group_user { association :group_user, group_id: group.id }
end

字符串

gtlvzcf8

gtlvzcf82#

我发现这对我很有效:

factory :group_feature_config do
  group_feature
  group_user { association :group_user, group: group_feature.group }
end

字符串
假设group_feature工厂设置了group

相关问题