Skip to content

Instantly share code, notes, and snippets.

@AMHOL
Last active October 6, 2016 16:45
Show Gist options
  • Save AMHOL/96c2908e93283eed9e4b5473964a24e1 to your computer and use it in GitHub Desktop.
Save AMHOL/96c2908e93283eed9e4b5473964a24e1 to your computer and use it in GitHub Desktop.
require 'bundler/inline'
gemfile(true) do
source 'https://rubygems.org'
gem 'rom', github: 'rom-rb/rom'
gem 'rom-sql', github: 'rom-rb/rom-sql'
gem 'rom-repository', github: 'rom-rb/rom-repository'
gem 'sqlite3'
end
config = ROM::Configuration.new(:sql, 'sqlite::memory')
gateway = config.gateways[:default]
migrations = []
migrations << gateway.migration do
change do
create_table :users do
primary_key :id
column :name, String, null: false
end
end
end
migrations << gateway.migration do
change do
create_table :tasks do
primary_key :id
foreign_key :user_id, :users, null: false, on_delete: :cascade
column :title, String
end
end
end
migrations << gateway.migration do
change do
create_table :tags do
primary_key :id
foreign_key :task_id, :tasks, null: false, on_delete: :cascade
column :name, String
end
end
end
migrations.each do |migration|
migration.apply(gateway.connection, :up)
end
class Users < ROM::Relation[:sql]
def by_id(id)
where(id: id)
end
end
class Tasks < ROM::Relation[:sql]
def by_id(id)
where(id: id)
end
end
class Tags < ROM::Relation[:sql]
def by_id(id)
where(id: id)
end
end
config.register_relation(Users)
config.register_relation(Tasks)
config.register_relation(Tags)
rom = ROM.container(config)
class UserRepo < ROM::Repository[:users]
relations :tasks, :tags
commands update: :by_id, delete: :by_id
def create(graph)
command(
:create,
aggregate(many: tasks.combine_children(many: tags))
).call(graph)
end
def [](id)
users.by_id(id).one
end
end
repo = UserRepo.new(rom)
user = repo.create(
name: 'Jane',
tasks: [{ title: 'Task', tags: [{ name: 'red' }]}]
)
@AMHOL
Copy link
Author

AMHOL commented Apr 8, 2016

TypeError: no implicit conversion of Symbol into Integer
    from /home/andy/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/bundler/gems/rom-c713d99e9ec3/lib/rom/commands/graph/input_evaluator.rb:47:in `fetch'
    from /home/andy/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/bundler/gems/rom-c713d99e9ec3/lib/rom/commands/graph/input_evaluator.rb:47:in `block in call'
    from /home/andy/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/bundler/gems/rom-c713d99e9ec3/lib/rom/commands/graph/input_evaluator.rb:47:in `each'
    from /home/andy/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/bundler/gems/rom-c713d99e9ec3/lib/rom/commands/graph/input_evaluator.rb:47:in `reduce'
    from /home/andy/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/bundler/gems/rom-c713d99e9ec3/lib/rom/commands/graph/input_evaluator.rb:47:in `call'
    from /home/andy/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/bundler/gems/rom-c713d99e9ec3/lib/rom/commands/lazy/create.rb:16:in `call'
    from /home/andy/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/bundler/gems/rom-c713d99e9ec3/lib/rom/commands/graph.rb:60:in `block in call'
    from /home/andy/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/bundler/gems/rom-c713d99e9ec3/lib/rom/commands/graph.rb:57:in `map'
    from /home/andy/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/bundler/gems/rom-c713d99e9ec3/lib/rom/commands/graph.rb:57:in `call'
    from /home/andy/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/bundler/gems/rom-c713d99e9ec3/lib/rom/commands/graph.rb:60:in `block in call'
    from /home/andy/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/bundler/gems/rom-c713d99e9ec3/lib/rom/commands/graph.rb:57:in `map'
    from /home/andy/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/bundler/gems/rom-c713d99e9ec3/lib/rom/commands/graph.rb:57:in `call'
    from /home/andy/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/bundler/gems/rom-c713d99e9ec3/lib/rom/relation/composite.rb:21:in `call'
    from /home/andy/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/bundler/gems/rom-repository-5307681774d5/lib/rom/repository/command_proxy.rb:14:in `call'
    from (irb):81:in `create'
    from (irb):91

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