Add _ids
has_many association array
#190
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What
This commit adds a getter and setter to has many associations for an
_ids
array. It returns this array into_params
and removes the association parameter.Why
If you have a user who has many posts and you send a
posts
parameter, AR will throw aActiveRecord::AssociationTypeMismatch
because it's expecting an association when you're sending a hash. You can either, instead, send alongpost_attributes
(to create the object and relationship) orpost_ids
(to create the relationship if the object already exists). The former was already present in Her; this commit adds the latter. This commit also avoids the AR error.How
A getter and setter is defined when attaching a has many association. The list of attributes when requesting
to_params
is filtered to remove the association array and add the_ids
array.This PR includes passing tests and has been integrated into a production app. This problem was discussed in #153 and credit to @calmyournerves for the code and tests to filter out the association array.
Example