Skip to content

Instantly share code, notes, and snippets.

@krisleech
Created April 24, 2023 16:10
Show Gist options
  • Save krisleech/162dad80bea35b4bf150d4877fb53d4c to your computer and use it in GitHub Desktop.
Save krisleech/162dad80bea35b4bf150d4877fb53d4c to your computer and use it in GitHub Desktop.
ephemeral ActiveRecord model for specs
let(:model_class) do
Class.new(ApplicationRecord) do
self.table_name = "model_class_#{SecureRandom.uuid.delete('-')}"
def self.up
connection.execute("CREATE TABLE #{table_name}(id INTEGER PRIMARY KEY AUTOINCREMENT)")
end
def self.down
ApplicationRecord.connection.execute("DROP TABLE #{table_name}")
end
end
end
around do |example|
model_class.up
example.run
model_class.down
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment