Skip to content

Instantly share code, notes, and snippets.

@pnomolos
Forked from solnic/ar_create.rb
Last active August 29, 2015 14:17
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 pnomolos/56684748c43a0ab68c8f to your computer and use it in GitHub Desktop.
Save pnomolos/56684748c43a0ab68c8f to your computer and use it in GitHub Desktop.
Call-tree example for ActiveRecord vs. rom-sql
require 'active_record'
require 'hotch'
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
ActiveRecord::Base.connection.execute("CREATE TABLE IF NOT EXISTS users(id int primary key, name varchar(255), email varchar(255), age int)");
class User < ActiveRecord::Base
end
User.delete_all
Hotch() do
100.times do
User.create(name: 'Jane Doe', email: 'jane@doe.org', age: 21)
end
end
# A sample Gemfile
source "https://rubygems.org"
gem 'activerecord'
gem 'hotch'
gem 'rom-sql'
gem 'sqlite3'
require 'rom-sql'
require 'hotch'
setup = ROM.setup(:sql, 'sqlite::memory')
setup.default.connection.create_table(:users) do
primary_key :id
String :name
String :email
Integer :age
end
# Right now this needs to be done manually as the table schema is read at the ROM.setup call
setup.default.instance_variable_set(:@schema, setup.default.connection.tables)
ROM.commands(:users) do
define(:create) do
result :one
end
end
rom = ROM.finalize.env
create_user = rom.command(:users).create
Hotch() do
100.times do
create_user.call(name: 'Jane Doe', email: 'jane@doe.org', age: 21)
end
end
@solnic
Copy link

solnic commented Mar 25, 2015

You didn't register any command so it doesn't exist in the registry. I'm not sure what you wanted to check but if you only want to see the command then defining relation and mapper is not needed (ROM infers relations automatically from db schema if you didn't define any).

@solnic
Copy link

solnic commented Mar 25, 2015

Looks like it's a gotcha - when you setup an adapter it looks at existing tables and stores that for later inference. In your example you create tables during setup so that won't work.

Hard to say if it's a bug or not. Hmm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment