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

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

Open
checkbutton opened this issue Jun 21, 2019 · 4 comments
Assignees
Labels

Comments

@checkbutton
Copy link

The following example returns a NoMethodError: undefined method 'one' for #<Hanami::Model::Associations::HasMany>:

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

Tested it on Hanami 1.3.1, according to this documentation:
https://guides.hanamirb.org/associations/has-many/#querying
It seems that it is also wrongly documented.

@landongrindheim
Copy link
Contributor

I've dug into this a little and would like to pitch in to resolve it 😄

There are a couple of ways this can be addressed, depending on whether #one is considered an appropriate method for HasMany.

If it shouldn't be supported, I can remove it from the docs. I noticed the code in the docs is also in place in the Hanami/Model repository.

Given this block of code...

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

... it seems reasonable to assume that there will be one book (since scoping is done on an id). However, if this scoping was done on a non-unique column/attribute, relying on ROM's one method would easily result in a TupleCountMismatchError. Perhaps a #first approach would be better suited to this problem?

@jodosha jodosha transferred this issue from hanami/hanami Sep 16, 2019
@jodosha jodosha added the bug label Sep 16, 2019
@jodosha
Copy link
Member

jodosha commented Sep 16, 2019

@mereghost Would you please have a look? Thanks.

landongrindheim added a commit to landongrindheim/model that referenced this issue Oct 2, 2019
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 added a commit to landongrindheim/model that referenced this issue Oct 2, 2019
Per hanami#550, #one is not implemented on on HasMany.
@landongrindheim
Copy link
Contributor

landongrindheim commented Oct 2, 2019

I'll provide two solutions to this. One adds the method #one to HasMany (#553), the other removes the documentation from this repository as well as the guides repository (#554) 🙂

@landongrindheim
Copy link
Contributor

@jodosha @mereghost Any thoughts on the solutions referenced above?

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