Skip to content

Instantly share code, notes, and snippets.

@mauricioklein
Last active July 16, 2016 00: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 mauricioklein/d585ac51e079667bc6eb260a1b9dccba to your computer and use it in GitHub Desktop.
Save mauricioklein/d585ac51e079667bc6eb260a1b9dccba to your computer and use it in GitHub Desktop.
Rom-SQL error
module Persistence
module Commands
class CreateUserCompany < ROM::Commands::Create[:sql]
input Dry::Data['hash'].schema(
user_id: 'int',
company_id: 'int'
)
relation :user_company
register_as :create
result :one
end
end
end
DROP TABLE IF EXISTS `user_company`;
CREATE TABLE `user_company` (
`user_id` bigint(20) NOT NULL,
`company_id` bigint(20) NOT NULL,
PRIMARY KEY (`user_id`,`company_id`),
KEY `fk_user_company_company_02` (`company_id`),
CONSTRAINT `fk_user_company_user_01` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`),
CONSTRAINT `fk_user_company_company_02` FOREIGN KEY (`company_id`) REFERENCES `company` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
require 'equalizer'
require 'dry/data'
module Entities
class UserCompany < Dry::Data::Struct
include Equalizer.new(:user_id, :company_id)
attribute :user_id, 'int'
attribute :company_id, 'int'
end
end
module Persistence
module Relations
class UserCompanies < ROM::Relation[:sql]
dataset :user_company do
order(:user_id)
end
primary_key [:user_id, :company_id]
end
end
end
module Persistence
module Repositories
class UserCompanies < ROM::Repository
relations :user_company
def by_user(user_id)
user_company.where(user_id: user_id).as(Entities::UserCompany).one
end
def by_company(company_id)
user_company.where(company_id: company_id).as(Entities::UserCompany).one
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment