Skip to content

Instantly share code, notes, and snippets.

@carpodaster
Created November 20, 2013 11:35
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 carpodaster/7561755 to your computer and use it in GitHub Desktop.
Save carpodaster/7561755 to your computer and use it in GitHub Desktop.
Dynamically create an ActiveRecord class with an in-memory database
ActiveRecord::Base.establish_connection adapter: 'sqlite3', database: ':memory:'
ActiveRecord::Base.connection.execute <<-EOSQL
CREATE TABLE in_memory_records(
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
name TEXT
);
EOSQL
Object.const_set 'InMemoryRecord', Class.new(ActiveRecord::Base)
InMemoryRecord.create name: 'Foo'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment