Last active
November 12, 2015 14:52
-
-
Save solnic/9307e1b2e3428718dd12 to your computer and use it in GitHub Desktop.
many-to-many with rom repo
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
I, [2015-11-11T18:21:57.978652 #52590] INFO -- : (0.000162s) CREATE TABLE `users` (`id` integer NOT NULL PRIMARY KEY AUTOINCREMENT, `name` varchar(255)) | |
I, [2015-11-11T18:21:57.978842 #52590] INFO -- : (0.000069s) CREATE TABLE `groups` (`id` integer NOT NULL PRIMARY KEY AUTOINCREMENT, `name` varchar(255)) | |
I, [2015-11-11T18:21:57.979020 #52590] INFO -- : (0.000062s) CREATE TABLE `users_groups` (`user_id` integer REFERENCES `users`, `group_id` integer REFERENCES `groups`) | |
I, [2015-11-11T18:21:57.979194 #52590] INFO -- : (0.000039s) INSERT INTO `users` (`id`, `name`) VALUES (1, 'Jane') | |
I, [2015-11-11T18:21:57.979321 #52590] INFO -- : (0.000027s) INSERT INTO `groups` (`id`, `name`) VALUES (1, 'Admins') | |
I, [2015-11-11T18:21:57.979398 #52590] INFO -- : (0.000025s) INSERT INTO `groups` (`id`, `name`) VALUES (2, 'Editors') | |
I, [2015-11-11T18:21:57.979486 #52590] INFO -- : (0.000025s) INSERT INTO `users_groups` (`user_id`, `group_id`) VALUES (1, 1) | |
I, [2015-11-11T18:21:57.979558 #52590] INFO -- : (0.000026s) INSERT INTO `users_groups` (`user_id`, `group_id`) VALUES (1, 2) | |
I, [2015-11-11T18:21:57.983559 #52590] INFO -- : (0.000073s) SELECT NULL AS 'nil' FROM `users` LIMIT 1 | |
I, [2015-11-11T18:21:57.983850 #52590] INFO -- : (0.000118s) PRAGMA table_info('users') | |
I, [2015-11-11T18:21:57.984432 #52590] INFO -- : (0.000043s) SELECT NULL AS 'nil' FROM `groups` LIMIT 1 | |
I, [2015-11-11T18:21:57.984604 #52590] INFO -- : (0.000057s) PRAGMA table_info('groups') | |
I, [2015-11-11T18:21:57.987130 #52590] INFO -- : (0.000072s) SELECT * FROM `users` | |
I, [2015-11-11T18:21:57.987572 #52590] INFO -- : (0.000081s) SELECT `groups`.`id`, `groups`.`name`, `users_groups`.`user_id` FROM `groups` INNER JOIN `users_groups` ON (`users_groups`.`group_id` = `groups`.`id`) WHERE (`user_id` IN (1)) | |
[#<ROM::Struct[User] id=1 name="Jane" groups=[#<ROM::Struct[Group] id=1 user_id=1 name="Admins">, #<ROM::Struct[Group] id=2 user_id=1 name="Editors">]>] |
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 'rom' | |
require 'rom-sql' | |
require 'rom-repository' | |
require 'logger' | |
ROM.use(:auto_registration) | |
ROM.setup(:sql, 'sqlite::memory') | |
conn = ROM::SQL.gateway.connection | |
conn.loggers << Logger.new($stdout) | |
conn.create_table(:users) do | |
primary_key(:id) | |
String(:name) | |
end | |
conn.create_table(:groups) do | |
primary_key(:id) | |
String(:name) | |
end | |
conn.create_table(:users_groups) do | |
foreign_key(:user_id, :users) | |
foreign_key(:group_id, :groups) | |
end | |
conn[:users].insert(id: 1, name: 'Jane') | |
conn[:groups].insert(id: 1, name: 'Admins') | |
conn[:groups].insert(id: 2, name: 'Editors') | |
conn[:users_groups].insert(user_id: 1, group_id: 1) | |
conn[:users_groups].insert(user_id: 1, group_id: 2) | |
class Users < ROM::Relation[:sql] | |
end | |
class Groups < ROM::Relation[:sql] | |
view(:for_users, [:id, :user_id, :name]) do |users| | |
select( | |
:groups__id, :groups__name, :users_groups__user_id | |
).inner_join( | |
:users_groups, group_id: :groups__id | |
).where( | |
user_id: users.map { |u| u[:id] } | |
) | |
end | |
end | |
class Repo < ROM::Repository::Base | |
relations :users, :groups | |
def users_with_groups | |
users.combine_children(many: groups.for_users).to_a | |
end | |
end | |
repo = Repo.new(ROM.finalize.env) | |
puts repo.users_with_groups.inspect |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I've not been able to use the view method as you show it here, i.e., it's reported as undefined. I used latest the gemfiles from Rubygems. I also tried switching to github/master, but at the time there seemed to be an unresolvable dependency issue between the many gems coming from /master. I was also unable to get repositories to load as even my simplest attempt reports an error about base names. Thanks for the help and this example, probably will work fine for others.