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

fix(active_record): ArgumentError in AssociationRelation #20

Merged

Conversation

aergonaut
Copy link
Contributor

This fix is specifically for AssociationRelation, which is the class that you get when you chain a where onto an association and then try to use methods such as first_or_initialize on the result.

In Rails 6, the signature of build, create, and create! changed. They no longer accept *args. See rails/rails@f1c1c43 for more details on that change.

When this gem's patch on first_or_initialize delegates to create, for example, it tries to pass options as a second parameter. This succeeded with the previous signature of these methods, but in Rails 6, this fails as the methods no longer accept a second argument.

This commit adds another patch to AssociationRelation to override these methods so that they accept the options argument and pass it on.

Fixes #19

This fix is specifically for `AssociationRelation`, which is the class
that you get when you chain a `where` onto an association and then try
to use methods such as `first_or_initialize` on the result.

In Rails 6, the signature of `build`, `create`, and `create!` changed.
They no longer accept `*args`. See
rails/rails@f1c1c43
for more details on that change.

When this gem's patch on `first_or_initialize` delegates to `create`,
for example, it tries to pass `options` as a second parameter. This
succeeded with the previous signature of these methods, but in Rails 6,
this fails as the methods no longer accept a second argument.

This commit adds another patch to `AssociationRelation` to override
these methods so that they accept the `options` argument and pass it on.
@westonganger
Copy link
Owner

Looks good. Great work.

@aergonaut aergonaut deleted the fix/association-relation-arguments branch October 24, 2020 01:46
westonganger added a commit that referenced this pull request Oct 24, 2020
@westonganger
Copy link
Owner

Just a heads up. You forgot to run Appraisal to ensure that the tests succeeded with all Rails versions.

I often forget this step as well, so in 60da8e1 I have now added a warning at the end of the default rake task regarding this.

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.

Calling first_or_initialize on an AssociationRelation raises ArgumentError in Rails 6
2 participants