-
-
Notifications
You must be signed in to change notification settings - Fork 152
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
Comments
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 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 |
@mereghost Would you please have a look? Thanks. |
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.
Per hanami#550, #one is not implemented on on HasMany.
@jodosha @mereghost Any thoughts on the solutions referenced above? |
The following example returns a
NoMethodError: undefined method 'one' for #<Hanami::Model::Associations::HasMany>
: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.
The text was updated successfully, but these errors were encountered: