Skip to content

Instantly share code, notes, and snippets.

@cflipse
Created October 28, 2017 18:42
Show Gist options
  • Save cflipse/8545ff9dbd7cffa1c48f7e7b7b5dab73 to your computer and use it in GitHub Desktop.
Save cflipse/8545ff9dbd7cffa1c48f7e7b7b5dab73 to your computer and use it in GitHub Desktop.
require 'bundler/inline'
gemfile(true) do
source "https://rubygems.org"
gem 'rom'
gem 'rom-sql'
gem 'sequel'
gem 'sqlite3'
end
config = ROM::Configuration.new(:sql, "sqlite::memory")
gateway = config.gateways[:default]
migration = gateway.migration do
change do
create_table :products do
primary_key :id
column :name, String, null: false
end
create_table :attachments do
primary_key :id
column :document_id, Integer, null: false
column :test_id, Integer, null: false
end
create_table :documents do
primary_key :id
column :name, String, null: false
end
end
end
migration.apply gateway.connection, :up
class ProductsRelation < ROM::Relation[:sql]
schema :products, infer: true do
associations do
has_many :attachments, foreign_key: :test_id
has_many :documents, through: :attachments
end
end
end
class AttachmentsRelation < ROM::Relation[:sql]
schema :attachments, infer: true do
associations do
belongs_to :document
belongs_to :product, foreign_key: :test_id
end
end
end
class DocumentsRelation < ROM::Relation[:sql]
schema :documents, infer: true
end
config.register_relation ProductsRelation
config.register_relation AttachmentsRelation
config.register_relation DocumentsRelation
rom = ROM.container(config)
rom.relations[:products].command(:create).call(name: "test 1", id: 1)
rom.relations[:documents].command(:create).call(name: 'test document', id: 100)
rom.relations[:attachments].command(:create).call(document_id: 100, test_id: 1)
puts rom.relations[:products].combine(:attachments).to_a
puts rom.relations[:attachments].combine(:documents).to_a
puts rom.relations[:products].combine(:documents).to_a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment