我在模型之间具有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