Skip to content
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

Merged
merged 8 commits into from May 22, 2017
Merged

Support for the Many-to-One association #398

merged 8 commits into from May 22, 2017

Conversation

mereghost
Copy link
Member

@mereghost mereghost commented Apr 25, 2017

Albeit we had belongs_to already it didn't really do anything. This PR implements it as far as we can.

class AuthorRepository < Hanami::Repository
  associations do
    has_many :books
  end
end

class BookRepository < Hanami::Repository
  associations do
    belongs_to :author  #the relation name is inferred by pluralization
  end

  # the authors relation is aliased to author on this scope
  def find_with_author(id)
    aggregate(:author).where(id: id).as(Book).one
  end

  def author_for(book)
    assoc(:author, book).one
  end
end

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. ;)

@mereghost mereghost requested a review from jodosha April 25, 2017 18:58
@mereghost mereghost added this to the v1.1.0 milestone Apr 26, 2017
@mereghost mereghost self-assigned this May 5, 2017
- 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.
@mereghost
Copy link
Member Author

Rebased over develop so I can pick the symbolize changes. =D

@AlfonsoUceda
Copy link
Contributor

AlfonsoUceda commented May 17, 2017

@mereghost 👍 It works perfect ;)

I want to share with you one thing I've found if you put a wrong relation name.

  • In belongs_to (belongs_to :autho):
 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
  • In has_many (has_many :book):
 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?

@jodosha
Copy link
Member

jodosha commented May 22, 2017

@AlfonsoUceda I'd unify these error conditions when we will implement other associations as well. /cc @mereghost

@jodosha
Copy link
Member

jodosha commented May 22, 2017

@mereghost I'm gonna merge this now. Please document this new feature on the Guides, using this PR: hanami/hanami.github.io#334

@jodosha jodosha merged commit 7d3f059 into develop May 22, 2017
@jodosha jodosha deleted the belongs_to branch May 22, 2017 12:55
@sovetnik
Copy link

sovetnik commented May 22, 2017

i'am on belongs_to branch found this behaviour:
Assume we have two models and relations between them:
User has_many Projects
Project belongs_to User

When we aggregate users from projects, aggregated entities (users) becomes ROM::Struct like #<ROM::Struct[User]… instead a User instance?

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>}>]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants