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

Add #one to HasMany (#550) #553

Conversation

landongrindheim
Copy link
Contributor

@landongrindheim landongrindheim commented Oct 2, 2019

Per #550, the HasMany interface
does not line up with Hanami's guides. Prior to this commit, the
following is invalid as HasMany does not implement #one.

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

  def find_book(author, id)
    book_for(author, id).one
  end

  private

  def book_for(author, id)
    assoc(:books, author).where(id: id)
  end
end

This implementation is based on a couple of others, notably ROM's and
Ecto's (Elixir). I opted to name the raised error MultipleResultsError,
which is the exact name used by Ecto, because I feel it bears a semantic
relationship to the name of the method from which raises it.

Note: This is one of two solutions to the linked issue, the other being #554. If either is accepted, the other PR should be closed.

Closes #550

Per hanami#550, the HasMany interface
does not line up with Hanami's guides. Prior to this commit, the
following is invalid as HasMany does not implement #one.

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

  def find_book(author, id)
    book_for(author, id).one
  end

  private

  def book_for(author, id)
    assoc(:books, author).where(id: id)
  end
end
```

This implementation is based on a couple of others, notably ROM's and
Ecto's (Elixir). I opted to name the raised error MultipleResultsError,
which is the exact name used by Ecto, because I feel it bears a semantic
relationship to the name of the method from which raises it.
@landongrindheim
Copy link
Contributor Author

@jodosha Could you let me know if this (or #554) is something you're interested in? If not, I'll close the PRs to clear them from my GitHub dashboard.

@landongrindheim
Copy link
Contributor Author

Closing this pull request to clear it from my GitHub dashboard.

@jodosha feel free to re-open this if you're interested in using this to address issue #550

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

Successfully merging this pull request may close these issues.

NoMethodError: undefined method `one' for #<Hanami::Model::Associations::HasMany>
1 participant