Skip to content

Instantly share code, notes, and snippets.

@ikraamg
Last active January 19, 2023 10:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ikraamg/a55f9270bfa30d7d1ddfd1b2a53f8f04 to your computer and use it in GitHub Desktop.
Save ikraamg/a55f9270bfa30d7d1ddfd1b2a53f8f04 to your computer and use it in GitHub Desktop.
How to use factory bot in the console and also receive the errors messages from failed creations
#In rails console:
include FactoryBot::Syntax::Methods
#From: https://github.com/thoughtbot/factory_bot/issues/397#issuecomment-6662051
FactoryBot.define do
to_create do |instance|
unless instance.save
raise "Invalid record: " + instance.errors.messages.to_s
end
end
end
# Now can use create(:<factory_name>) and if it fails it will return the errors if it fails to create.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment