Last active
July 16, 2016 00:17
-
-
Save mauricioklein/d585ac51e079667bc6eb260a1b9dccba to your computer and use it in GitHub Desktop.
Rom-SQL error
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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