Skip to content

Instantly share code, notes, and snippets.

@solnic
Created April 27, 2016 12:15
Show Gist options
  • Save solnic/94b9e952b75b1f4605103c9e2f44515b to your computer and use it in GitHub Desktop.
Save solnic/94b9e952b75b1f4605103c9e2f44515b to your computer and use it in GitHub Desktop.
class Users < ROM::Relation[:users]
schema do
attribute :id, Types::Serial
attribute :name, Types::String
associate do
many :posts
end
end
end
class Posts < ROM::Relation[:posts]
schema do
attribute :id, Types::Serial
attribute :user_id, Types::ForeignKey(:users)
attribute :title, Types::String
attribute :body, Types::String
associate do
many :tags, through: :posts_tags
end
end
end
class UserRepo < ROM::Repository[:users]
end
class PostRepo < ROM::Repository[:posts]
end
user_repo = UserRepo.new(rom_container)
post_repo = PostRepo.new(rom_container)
user_repo.aggregate(:posts) # returns users with their posts
post_repo.aggregate(:tags) # returns posts with their tags
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment