Skip to content

Instantly share code, notes, and snippets.

@ClayShentrup
Last active September 2, 2018 05:59
Show Gist options
  • Save ClayShentrup/b5d4dc711989b0fad3a99d2ae445367d to your computer and use it in GitHub Desktop.
Save ClayShentrup/b5d4dc711989b0fad3a99d2ae445367d to your computer and use it in GitHub Desktop.
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