Skip to content

Instantly share code, notes, and snippets.

@solnic
Created February 9, 2017 19:23
Show Gist options
  • Save solnic/fedd5c41e9892abc79b46bffafa06482 to your computer and use it in GitHub Desktop.
Save solnic/fedd5c41e9892abc79b46bffafa06482 to your computer and use it in GitHub Desktop.
require 'rom-sql'
require 'rom-repository'
rom = ROM.container(:sql, 'sqlite::memory') do |c|
c.gateways[:default].create_table :things do
primary_key :id
column :first_id, Integer
column :second_id, Integer
column :name, String
end
c.gateways[:default].create_table :drawers do
primary_key :id
column :first_id, Integer
column :second_id, Integer
column :name, String
end
c.gateways[:default].use_logger(Logger.new($stdout))
c.relation(:things) do
schema(infer: true)
view(:for_drawers) do
schema do
append(relations[:drawers][:id].as(:drawer_id)).qualified
end
relation do |drawers|
things.
join(:drawers,
drawers.source[:first_id].qualified => self[:first_id].qualified,
drawers.source[:second_id].qualified => self[:second_id].qualified).
where(drawers.source[:id].qualified => drawers.pluck(:id))
end
end
end
c.relation(:drawers) do
schema(infer: true)
end
end
rom.relations[:drawers].insert name: 'First Drawer', first_id: 1, second_id: 2
rom.relations[:drawers].insert name: 'Second Drawer', first_id: 3, second_id: 4
rom.relations[:things].insert(name: 'A thing from the first drawer', first_id: 1, second_id: 2)
rom.relations[:things].insert(name: 'A thing from the second drawer', first_id: 3, second_id: 4)
class DrawerRepo < ROM::Repository[:drawers]
relations :things
end
drawer_repo = DrawerRepo.new(rom)
puts drawer_repo.drawers.combine(many: drawer_repo.things.for_drawers).to_a.inspect
# SELECT `id`, `first_id`, `second_id`, `name` FROM `drawers` ORDER BY `drawers`.`id`
# SELECT `things`.`id`, `things`.`first_id`, `things`.`second_id`, `things`.`name`, `drawers`.`id` AS 'drawer_id' FROM `things` INNER JOIN `drawers` ON ((`drawers`.`first_id` = `things`.`first_id`) AND (`drawers`.`second_id` = `things`.`second_id`)) WHERE (`drawers`.`id` IN (1, 2)) ORDER BY `things`.`id`
# [#<ROM::Struct[Drawer] id=1 first_id=1 second_id=2 name="First Drawer" things=[#<ROM::Struct[Thing] id=1 first_id=1 second_id=2 name="A thing from the first drawer" drawer_id=1>]>, #<ROM::Struct[Drawer] id=2 first_id=3 second_id=4 name="Second Drawer" things=[#<ROM::Struct[Thing] id=2 first_id=3 second_id=4 name="A thing from the second drawer" drawer_id=2>]>]
@solnic
Copy link
Author

solnic commented Feb 10, 2017

You can specify combine keys explicitly ie:

drawer_repo.drawers.combine(many: { drawers: [drawer_repo.things.for_drawers, key_on_parent_side: key_on_child_side] })

@mrship
Copy link

mrship commented Feb 13, 2017

@solnic, thanks - I've made it work on that basis.

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