称其他工厂的STI工厂模型

时间:2019-11-16 15:02:58

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

我在模型之间具有STI关联(如下)

class Content < ApplicationRecord
end

class Gallery < Content
  has_many :gallery_images, dependent: :destroy
end

class Video < Content
end

该工厂(spec / factories / contents.rb)

FactoryBot.define do
  factory :content, class: 'Content' do
  organization //isn`t important

  trait :video do
    type { 'Video' }
    video_url { 'https://www.youtube.com/watch?v=IWZ_71EKbng&t=5s' }
  end

  trait :gallery do
    type { 'Gallery' }
    video_url { Faker::Internet.url }
  end
end
end

在这里,我为Content, Gallery, Video模型创建工厂。但是gallery模型仍然具有指向gallery_images的链接。我如何在gallery工厂中调用gallery_images

类似这样的东西

FactoryBot.define do
  factory :gallery_image, class: GalleryImage do
    image { Faker::Lorem.sentence }
    // gallery or create(:content, :gallery)
  end
end

0 个答案:

没有答案