Implementing many-to-many (and other) relationships with Hanami - hanami

I searched the docs for how to implement relationships among entities (eg, one-to-many, many-to-many, etc), but didn't find any examples.
So I tried a reasonable guess. Here's my attempt at implementing a Person who can be tagged with Tags:
require 'moocho_query'
require 'hanami/model'
require 'hanami/model/adapters/file_system_adapter'
class Person
include Hanami::Entity
attributes :name, :age, :tags
end
class Tag
include Hanami::Entity
attributes :name
end
class PersonRepository
include Hanami::Repository
end
class TagRepository
include Hanami::Repository
end
Hanami::Model.configure do
adapter type: :file_system, uri: './file_db'
mapping do
collection :people do
entity Person
repository PersonRepository
attribute :id, Integer
attribute :name, String
attribute :age, Integer
attribute :tags, type: Array[Tag]
end
collection :tags do
entity Tag
repository TagRepository
attribute :id, Integer
attribute :name, String
end
end
end.load!
me = Person.new(name: 'Jonah', age: 99)
t1 = Tag.new(name: 'programmer')
t2 = Tag.new(name: 'nice')
me.tags = [t1, t2]
PersonRepository.create(me)
This fails on the load! call, with the following error:
/Users/x/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/hanami-utils-0.7.0/lib/hanami/utils/class.rb:90:in `load_from_pattern!': uninitialized constant (Hanami::Model::Mapping::Coercers::{:type=>[Tag]}|
{:type=>[Tag]}) (NameError)
from /Users/jg/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/hanami-model-0.6.0/lib/hanami/model/mapping/attribute.rb:80:in `coercer'
from /Users/jg/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/hanami-model-0.6.0/lib/hanami/model/mapping/attribute.rb:53:in `load_coercer'
What is the correct way to implement relationships in Hanami?

As of version 0.7.0, there is no way to implement relationships between the entities. This is why there is no how-to in the documentation as well.
Out of curiousity, I had inquired this using a tweet which can be taken as an official word on entity relationships.
As a work around, in Hanami, entities are simply objects that you are persisting to the database which means an entity's persistence details can vary from it's schema.
I would suggest having a tags method on the Person object. Inside this method, you can retrieve the person's tags. Something like this:
def self.tags
TagRepository.query do
where(id: [tag-id-1, tag-id-2, ... , tag-id-n])
end.all
end
Though you will need to persist the tag ids associated with the person to the database as an attribute of the person or using a join table.
Do know that this implementation will have the n+1 query problem.

I know it's an old question, but I'll leave this answer in case somebody stumbles here:
Hanami added many-to-many support on version 1.1
http://hanamirb.org/guides/1.1/associations/has-many-through/
Basic setup
% bundle exec hanami generate model user
create lib/bookshelf/entities/user.rb
create lib/bookshelf/repositories/user_repository.rb
create db/migrations/20171024083639_create_users.rb
create spec/bookshelf/entities/user_spec.rb
create spec/bookshelf/repositories/user_repository_spec.rb
% bundle exec hanami generate model story
create lib/bookshelf/entities/story.rb
create lib/bookshelf/repositories/story_repository.rb
create db/migrations/20171024085712_create_stories.rb
create spec/bookshelf/entities/story_spec.rb
create spec/bookshelf/repositories/story_repository_spec.rb
% bundle exec hanami generate model comment
create lib/bookshelf/entities/comment.rb
create lib/bookshelf/repositories/comment_repository.rb
create db/migrations/20171024085858_create_comments.rb
create spec/bookshelf/entities/comment_spec.rb
create spec/bookshelf/repositories/comment_repository_spec.rb
Migrations
Users table:
# db/migrations/20171024083639_create_users.rb
Hanami::Model.migration do
change do
create_table :users do
primary_key :id
column :name, String, null: false
column :created_at, DateTime, null: false
column :updated_at, DateTime, null: false
end
end
end
Stories table:
# db/migrations/20171024085712_create_stories.rb
Hanami::Model.migration do
change do
create_table :stories do
primary_key :id
foreign_key :user_id, :users, null: false, on_delete: :cascade
column :text, String, null: false
column :created_at, DateTime, null: false
column :updated_at, DateTime, null: false
end
end
end
Comments table:
# db/migrations/20171024085858_create_comments.rb
Hanami::Model.migration do
change do
create_table :comments do
primary_key :id
foreign_key :user_id, :users, null: false, on_delete: :cascade
foreign_key :story_id, :stories, null: false, on_delete: :cascade
column :text, String, null: false
column :created_at, DateTime, null: false
column :updated_at, DateTime, null: false
end
end
end
Repositories
User repository:
# lib/bookshelf/repositories/user_repository.rb
class UserRepository < Hanami::Repository
associations do
has_many :stories
has_many :comments
end
end
Story repository:
# lib/bookshelf/repositories/story_repository.rb
class StoryRepository < Hanami::Repository
associations do
belongs_to :user
has_many :comments
has_many :users, through: :comments
end
def find_with_comments(id)
aggregate(:user, comments: :user).where(id: id).map_to(Story).one
end
def find_with_commenters(id)
aggregate(:users).where(id: id).map_to(Story).one
end
end
Comment repository:
# lib/bookshelf/repositories/comment_repository.rb
class CommentRepository < Hanami::Repository
associations do
belongs_to :story
belongs_to :user
end
end
Usage
user_repo = UserRepository.new
author = user_repo.create(name: "Luca")
# => #<User:0x00007ffe71bc3b18 #attributes={:id=>1, :name=>"Luca", :created_at=>2017-10-24 09:06:57 UTC, :updated_at=>2017-10-24 09:06:57 UTC}>
commenter = user_repo.create(name: "Maria G")
# => #<User:0x00007ffe71bb3010 #attributes={:id=>2, :name=>"Maria G", :created_at=>2017-10-24 09:07:16 UTC, :updated_at=>2017-10-24 09:07:16 UTC}>
story_repo = StoryRepository.new
story_repo.create(user_id: author.id, text: "Hello, folks")
# => #<Story:0x00007ffe71b4ace0 #attributes={:id=>1, :user_id=>1, :text=>"Hello folks", :created_at=>2017-10-24 09:09:59 UTC, :updated_at=>2017-10-24 09:09:59 UTC}>
story = story_repo.find_with_comments(story.id)
# => #<Story:0x00007fd45e327e60 #attributes={:id=>2, :user_id=>1, :text=>"Hello folks", :created_at=>2017-10-24 09:09:59 UTC, :updated_at=>2017-10-24 09:09:59 UTC, :user=>#<User:0x00007fd45e326bc8 #attributes={:id=>1, :name=>"Luca", :created_at=>2017-10-24 09:06:57 UTC, :updated_at=>2017-10-24 09:06:57 UTC}>, :comments=>[#<Comment:0x00007fd45e325930 #attributes={:id=>1, :user_id=>2, :story_id=>2, :text=>"Hi and welcome!", :created_at=>2017-10-24 09:12:30 UTC, :updated_at=>2017-10-24 09:12:30 UTC, :user=>#<User:0x00007fd45e324490 #attributes={:id=>2, :name=>"Maria G", :created_at=>2017-10-24 09:07:16 UTC, :updated_at=>2017-10-24 09:07:16 UTC}>}>]}>
story.comments
# => [#<Comment:0x00007fe289f2d618 #attributes={:id=>1, :user_id=>2, :story_id=>2, :text=>"Hi and welcome!", :created_at=>2017-10-24 09:12:30 UTC, :updated_at=>2017-10-24 09:12:30 UTC, :commenter=>#<User:0x00007fe289f2c420 #attributes={:id=>2, :name=>"Maria G", :created_at=>2017-10-24 09:07:16 UTC, :updated_at=>2017-10-24 09:07:16 UTC}>}>]

as of Hanami 0.9.0, there is the initial support for associations. The first one is one-to-many. Please check it and see if it makes sense for you. Thanks

Related

Why is this join table always doing posts_tags?

When I use the following:
class CreateJoinTableTagsPosts < ActiveRecord::Migration
def change
create_join_table :tags, :posts do |t|
t.index [:tag_id, :post_id]
end
end
end
or the following:
class CreateJoinTableTagsPosts < ActiveRecord::Migration
def change
create_join_table :posts, :tags do |t|
t.index [:post_id, :tag_id]
end
end
end
I always get a table that is posts_tags and as a result helper methods in the posts model:
class Post < ActiveRecord::Base
belongs_to :blogs
has_and_belongs_to_many :tags, join_table: 'tags_posts'
has_and_belongs_to_many :categories, join_table: 'categories_posts'
has_many :comments
validates :title, presence: true
def has_tag?(tag_name)
tag == tag_name
end
def assign_tag=(tag_name)
tag = Tag.find_by(name: tag_name) || Tag.create(name: tag_name)
self.tag = [tag] if tag
end
end
Don't actually work. As you can see the assign_tag method wont actually work, tests that are done as such:
it "should create a page for a post" do
#post.assign_tag = 'Sample Tag'
end
fails because relation tags doesn't exist. I believe this can be solved by creating the appropriate join table of tags_posts instead of the one it always creates posts_tags
ideas?
create_join_table uses the lexical order of the arguments to name the table.
You can override this using a table_name option:
create_join_table :posts, :tags, table_name: 'tags_posts' do |t|
t.index [:post_id, :tag_id]
end

using activeuuid gem for existing database in rails

I need to implement activeuuid gem to have UUIDs instead of default Rails ids. we can implement it for creating new migration as:
class CreateStudents < ActiveRecord::Migration
def change
create_table :students, :id => false do |t|
t.uuid :id, :primary_key => true
t.string :name
t.string :email
t.timestamps
end
end
end
And in model we include ActiveUUID::UUID as:
class Student < ActiveRecord::Base
attr_accessible :email, :name
include ActiveUUID::UUID
end
Now I already have a database so how can I implement the activeuuid gem to have UUIDs instead of default Rails ids for existing DB?
Need to make changes in all migrations or what?
Need help in this regard. thanks
The UUID is stored as a binary field w/ 16 positions as I found here: https://github.com/jashmenn/activeuuid/blob/master/lib/activeuuid/patches.rb#L62
It worked for me (existing table without records):
def change
reversible do |dir|
change_table :payments do |t|
dir.up { t.change :id, :binary, limit: 16, :primary_key => true }
dir.down { t.change :id, :integer }
end
end
end
Don't forget to add those lines to your model as well:
include ActiveUUID::UUID
natural_key :at_least_one_field_here
More info in the github repo: https://github.com/jashmenn/activeuuid/

Rails 3 uniqueness validation with scope on polymorphic table

I've got the following setup:
class Vote < ActiveRecord::Base
belongs_to :voteable, :polymorphic => :true, :counter_cache => true
end
class Proposition < ActiveRecord::Base
has_many :votes, :as => :voteable
end
class Winner < ActiveRecord::Base
has_many :votes, :as => :voteable
end
The Vote table looks like this:
t.string "ip_address"
t.integer "voteable_id"
t.string "voteable_type"
I want to validate the following. A user with a given ip_address can only vote on 1 proposition. So the combination of ip_address, voteable_id and voteable_type needs to be unique.
How can i achieve this with a "simple" validation rule?
To guarantee uniqueness you have to add unique index to your DB
If you don't have important data yet you can do it inside migration with add_index
add_index(:votes, [:ip_address, :voteable_id, voteable_type], :unique => true, :name => 'allowed_one_vote')
in case you already have some data it can be done with SQL and it depends on your DBMS
Add a scope to a unique :ip_address validation
class Vote < ActiveRecord::Base
# ...
validates :ip_address, uniqueness: { scope: [:voteable_type, :voteable_id]}
end

passing object for polymorphic lookup parameter in Rails find/where

Let's say I have:
class Comment < ActiveRecord::Base
belongs_to :commentable, :polymorphic => true
end
class Article < ActiveRecord::Base
has_many :comments, :as => :commentable
end
class Photo < ActiveRecord::Base
has_many :comments, :as => :commentable
#...
end
now I want to find all comments on Jim's photo:
#jims_photo = Photo.where(:of => "Jim")
#photo_comments = Comment.where(:commentable => #jims_photo)
this seems to not work in rails (Rails 3). The generated query seems to not expand the polymorphic object into commentable_id and commentable_type fields:
SQLite3::SQLException: no such column: comments.commentable:
I'm new to ruby and rails so I might be using the paradigm incorrectly but my expectation was that rails automatically expands
:commentable => #jims_photo
to:
:commentable_id => #jims_photo.id, :commentable_type => #jims_photo.class.name
If you want to be really safe with:
:commentable_id => #jims_photo.id, :commentable_type => #jims_photo.class.name
Then I'd recommend replacing .class.name with .base_class (you don't actually need the name: to_s returns name and will be called automatically).
The reason for this is that when ActiveRecord saves the _type for a polymorphic association it'll use base_class to ensure it's not saving a class which itself is a polymorphic one.
If you play with store_full_sti_class you'll probably have to take even more precautions.
I highly recommend looking at Rails' STI code, here.
The guides for Rails are one of the best so I'd suggest you start reading about Polymorphic Associations
You class declarations looks OK and I'm assuming that you're migrations is as well. But just for the sake of it. Let's say it looks like this:
class CreateComment < ActiveRecord::Migration
def change
create_table :comments do |t|
t.string :name
t.references :commentable, :polymorphic => true
# this is the equivalent of
# t.integer :commentable_id
# t.string :commentable_type
t.timestamps
end
end
end
Not if you have a Article or Photo object and you want to get the comments for that object then Thilo's suggestion is right on. All you need to do is this: #jims_photo.comments
If, on the other hand, you have a an instance of the Comment model, you can get the parent like this: #comment.commentable. But if you want to get Jim's photo comments best to do it like that. Otherwise, you'd have to supply as arguments both the :commentable_id and commentable_type. I'm not aware of any finder that expands the polymorphic object into commentable_id and commentable_type fields for you.
But you can always create a class method for that:
def self.find_by_parent(parent)
where(:commentable_id => parent.id, :commentable_type => parent.class.name)
end

Mixing has_one and has_and_belongs_to_many associations

I'm trying to build a database of urls(links). I have a Category model that has and belongs to many Links.
Here's the migration I ran:
class CreateLinksCategories < ActiveRecord::Migration
def self.up
create_table :links_categories, :id => false do |t|
t.references :link
t.references :category
end
end
def self.down
drop_table :links_categories
end
end
Here's the Link model:
class Link < ActiveRecord::Base
validates :path, :presence => true, :format => { :with => /^(#{URI::regexp(%w(http
https))})$|^$/ }
validates :name, :presence => true
has_one :category
end
Here's the category model:
class Category < ActiveRecord::Base
has_and_belongs_to_many :links
end
And here's the error the console kicked back when I tried to associate the first link with the first category:
>>link = Link.first
=> #<Link id: 1, path: "http://www.yahoo.com", created_at: "2011-01-10...
>>category = Category.first
=> #<category id : 1, name: "News Site", created_at: "2011-01-11...
>>link.category << category
=> ActiveRecord::StatementInvalid: SQLite3::Exception: no such column :
categories.link_id:
Are my associations wrong or am I missing something in the database? I expected it to find the links_categories table. Any help is appreciated.
Why are you using HABTM here at all? Just put a belongs_to :category on Link, and a has_many :links on Category. Then in the db, you don't need a join table at all, just a :category_id on the links table.
But, if you do want a HABTM here, from a quick glance, the first thing I noticed is that your join table is named incorrectly -- it should be alphabetical, categories_links.
The second thing is that you can't mix has_one and has_and_belongs_to_many. HABTM means just that -- A has many of B and A belongs to many of B; this relationship implies that the opposite must also be true -- B has many of A and B belongs to many of A. If links HABTM cateogries, then categories must HABTM links.
See http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#method-i-has_and_belongs_to_many