Skip to content

Instantly share code, notes, and snippets.

@ClayShentrup
Last active Sep 2, 2018
Embed
What would you like to do?
Three ways to do STI factories
# assets.rb
FactoryBot.define do
factory(:asset) do
# definition
end
end
# audio_assets.rb
FactoryBot.define do
factory(:audio_asset, parent: :asset, class: AudioAsset) do
# definition
end
end
# assets.rb
FactoryBot.define do
trait(:asset) do
# definition
end
factory(:audio_asset, traits: %i[asset]) do
# definition
end
# other subclasses, like video_asset, defined here too
end
# shared_traits.rb
FactoryBot.define do
trait(:asset) do
# definition
end
end
# audio_assets.rb
FactoryBot.define do
factory(:audio_asset) do
asset # Ambiguous: you have to know this is a trait, not an association
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment