Skip to content

Instantly share code, notes, and snippets.

@blelump
Created March 21, 2018 22:56
Show Gist options
  • Save blelump/a0d1fe6d5a27b617462346150e442e79 to your computer and use it in GitHub Desktop.
Save blelump/a0d1fe6d5a27b617462346150e442e79 to your computer and use it in GitHub Desktop.
rom = ROM.container(:sql, 'sqlite::memory') do |conf|
conf.gateways[:default].use_logger(Logger.new($stdout))
conf.default.create_table(:users) do
primary_key :id
column :login, String
end
conf.default.create_table(:tasks) do
primary_key :id
foreign_key :user_id, :users
column :name, String, null: false
end
conf.relation(:users) do
schema(:users, infer: true) do
end
end
conf.relation(:tasks) do
schema(:tasks, infer: true) do
end
def for_users(users)
# where(employee_id: users.map { |tuple| tuple[:employee_id] })
where(user_id: 1)
end
end
end
user = rom.relations[:users]
.changeset(:create, login: '[email protected]')
.commit
rom.relations[:tasks].changeset(
:create,
[
{ name: 'ble ble', user_id: user[:id] },
{ name: 'ble', user_id: user[:id] }
]
).commit
p rom.relations[:users].combine_with(rom.relations[:tasks].for_users).to_a
# It fetches both relations:
# INFO -- : (0.000088s) SELECT `users`.`id`, `users`.`login` FROM `users` ORDER BY `users`.`id`
# INFO -- : (0.000106s) SELECT `tasks`.`id`, `tasks`.`user_id`, `tasks`.`name` FROM `tasks` WHERE (`user_id` = 1) ORDER BY `tasks`.`id`
# buth finally fails probably on mapping:
/work/.rvm/gems/ruby-2.3.4@isa/gems/rom-mapper-1.1.0/lib/rom/processor/transproc.rb:40:in `inject_union_value': [[:id], [:user_id], [:name]] attribute: block is required for :from with union value. (ROM::MapperMisconfiguredError)
from /work/.rvm/gems/ruby-2.3.4@isa/gems/transproc-1.0.2/lib/transproc/function.rb:47:in `call'
from /work/.rvm/gems/ruby-2.3.4@isa/gems/transproc-1.0.2/lib/transproc/function.rb:47:in `call'
from /work/.rvm/gems/ruby-2.3.4@isa/gems/transproc-1.0.2/lib/transproc/composite.rb:30:in `call'
from /work/.rvm/gems/ruby-2.3.4@isa/gems/transproc-1.0.2/lib/transproc/array.rb:42:in `block in map_array'
from /work/.rvm/gems/ruby-2.3.4@isa/gems/transproc-1.0.2/lib/transproc/array.rb:42:in `map'
from /work/.rvm/gems/ruby-2.3.4@isa/gems/transproc-1.0.2/lib/transproc/array.rb:42:in `map_array'
from /work/.rvm/gems/ruby-2.3.4@isa/gems/transproc-1.0.2/lib/transproc/function.rb:47:in `call'
from /work/.rvm/gems/ruby-2.3.4@isa/gems/transproc-1.0.2/lib/transproc/function.rb:47:in `call'
from /work/.rvm/gems/ruby-2.3.4@isa/gems/rom-mapper-1.1.0/lib/rom/mapper.rb:95:in `block in call'
from /work/.rvm/gems/ruby-2.3.4@isa/gems/rom-mapper-1.1.0/lib/rom/mapper.rb:95:in `each'
from /work/.rvm/gems/ruby-2.3.4@isa/gems/rom-mapper-1.1.0/lib/rom/mapper.rb:95:in `reduce'
from /work/.rvm/gems/ruby-2.3.4@isa/gems/rom-mapper-1.1.0/lib/rom/mapper.rb:95:in `call'
from /work/.rvm/gems/ruby-2.3.4@isa/gems/rom-core-4.1.0/lib/rom/relation/combined.rb:64:in `call'
from /work/.rvm/gems/ruby-2.3.4@isa/gems/rom-core-4.1.0/lib/rom/relation/materializable.rb:13:in `to_a'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment