-
Notifications
You must be signed in to change notification settings - Fork 1.4k
MONGOID 5800 When initializing an embedded association with an invalid hash, got undefined method `with_indifferent_access' error #6075
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
Changes from all commits
60c3236
6b7d9bb
4b62cd6
8d417bb
e61cbd2
d7099a5
2647510
bef124d
82b5786
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,6 +16,9 @@ class Many | |
| # This attempts to perform 3 operations, either one of an update of | ||
| # the existing association, a replacement of the association with a new | ||
| # document, or a removal of the association. | ||
| # | ||
| # It raises an argument error if the attributes are not a Hash or an | ||
| # Array of key/value pairs. | ||
| # | ||
| # @example Build the nested attrs. | ||
| # many.build(person) | ||
|
|
@@ -32,8 +35,12 @@ def build(parent, options = {}) | |
| attributes.each do |attrs| | ||
| if attrs.is_a?(::Hash) | ||
| process_attributes(parent, attrs.with_indifferent_access) | ||
| else | ||
| elsif attrs.is_a?(Array) && attrs.length > 1 && attrs[1].respond_to?(:with_indifferent_access) | ||
| process_attributes(parent, attrs[1].with_indifferent_access) | ||
| elsif attrs.is_a?(Array) && attrs.length.even? | ||
| process_attributes(parent, Hash[*attrs].with_indifferent_access) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we need more guards here in case it can't be cast to a hash?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's probably smart to check that array has an even number of elements, before creating a hash from it. I think an empty Hash is okay here -- I can imagine someone doing We should probably also raise an exception in the final
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. agreed - I didn't add any extra testing for this, I can't think of any scenarios where an uneven number of fields would be provided, but I agree that it's a good thing to check for. |
||
| else | ||
| raise ArgumentError, "Attributes for nested association '#{association.name}' must be a Hash or an Array of key/value pairs." | ||
| end | ||
| end | ||
| end | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using
Hash[*attrs]assumesattrsis an array with an even number of elements (key-value pairs). Ifattrshas an odd number of elements, this will raise anArgumentError. Add validation or rescue logic to handle malformed input gracefully.