New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for the Many-to-One association #398
Conversation
- belongs_to will now instantiate a Hanami::Associations::BelongsToAssociation - The relation name will be pluralized internally
Start to implement the BelongsTo#create method.
…oken test. Expands a bit the belongs_to tests.
Rebased over |
@mereghost 👍 It works perfect ;) I want to share with you one thing I've found if you put a wrong relation name.
bookshelf git:(master) ✗ be hanami console
bundler: failed to load command: hanami (/Users/alfonsouceda/.gem/ruby/2.3.3/bin/hanami)
Hanami::Model::Error: key not found: :autho
bookshelf git:(master) ✗ be hanami console
irb(main):001:0> authors = AuthorRepository.new
ROM::Registry::ElementNotFoundError: :book doesn't exist in ROM::RelationRegistry registry Notice the difference, can we unify them? I think we don't need to do it here, we can do it in another PR. What do you think @jodosha? |
@AlfonsoUceda I'd unify these error conditions when we will implement other associations as well. /cc @mereghost |
@mereghost I'm gonna merge this now. Please document this new feature on the Guides, using this PR: hanami/hanami.github.io#334 |
i'am on When we aggregate users from projects, aggregated entities (users) becomes ROM::Struct like class UserRepository < Hanami::Repository
associations do
has_many :projects
end
end
class ProjectRepository < Hanami::Repository
associations do
belongs_to :user
end
end
> UserRepository.new.aggregate(:projects).as(User).one
=> #<User:0x007faa23217aa8
@attributes=
{:id=>1, :name=>"Oleg", :nickname=>"sovetnik", :created_at=>2017-05-13 12:26:22 UTC, :updated_at=>2017-05-13 12:26:22 UTC, :projects=>[
#<Project:0x007faa22e183a8 @attributes={:id=>2, :user_id=>1, :head_id=>nil, :name=>"Chaos"}>,
#<Project:0x007faa22e2b570 @attributes={:id=>3, :user_id=>1, :head_id=>nil, :name=>"Discord"}>
]}>
> ProjectRepository.new.aggregate(:user).as(Project).to_a
=> [#<Project:0x007faa224a4d58
@attributes=
{:id=>2, :user_id=>1, :head_id=>nil, :name=>"Chaos", :user=>#<ROM::Struct[User] id=1 name="Oleg" nickname="sovetnik" created_at=2017-05-13 12:26:22 UTC updated_at=2017-05-13 12:26:22 UTC project_id=3>}>,
#<Project:0x007faa224ad7c8
@attributes=
{:id=>3, :user_id=>1, :head_id=>nil, :name=>"Discord", :user=>#<ROM::Struct[User] id=1 name="Oleg" nickname="sovetnik" created_at=2017-05-13 12:26:22 UTC updated_at=2017-05-13 12:26:22 UTC project_id=3>}>] |
Albeit we had
belongs_to
already it didn't really do anything. This PR implements it as far as we can.Note that we don't support the creation of a parent record from a child one.
@hanami/core anything missing?
PS: The changes on the ConstraintViolationErrors were mainly due to things being broken on my machine and that... well it makes sense. ;)