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

Generate delegation methods to named scope in the definition time #34122

Merged
merged 1 commit into from
Oct 10, 2018

Conversation

kamipo
Copy link
Member

@kamipo kamipo commented Oct 8, 2018

The delegation methods to named scope are defined when method_missing
is invoked on the relation.

Since #29301, the receiver in the named scope is changed to the relation
like others (e.g. default_scope, etc) for consistency.

Most named scopes would be delegated from relation by method_missing,
since we don't allow scopes to be defined which conflict with instance
methods on Relation (#31179). But if a named scope is defined with the
same name as any method on the superclass (e.g. Kernel.open), the
method_missing on the relation is not invoked.

To address the issue, make the delegation methods to named scope is
generated in the definition time.

Fixes #34098.

The delegation methods to named scope are defined when `method_missing`
is invoked on the relation.

Since rails#29301, the receiver in the named scope is changed to the relation
like others (e.g. `default_scope`, etc) for consistency.

Most named scopes would be delegated from relation by `method_missing`,
since we don't allow scopes to be defined which conflict with instance
methods on `Relation` (rails#31179). But if a named scope is defined with the
same name as any method on the `superclass` (e.g. `Kernel.open`), the
`method_missing` on the relation is not invoked.

To address the issue, make the delegation methods to named scope is
generated in the definition time.

Fixes rails#34098.
@kamipo kamipo merged commit a52c698 into rails:master Oct 10, 2018
@kamipo kamipo deleted the generate_relation_methods branch October 10, 2018 07:59
@rafaelfranca
Copy link
Member

@kamipo do you need to backport?

kamipo added a commit that referenced this pull request Oct 10, 2018
Generate delegation methods to named scope in the definition time
@kamipo
Copy link
Member Author

kamipo commented Oct 10, 2018

Yeah, backported 0fcde6d.

kamipo added a commit to kamipo/rails that referenced this pull request May 9, 2020
Follow up of rails#34122.

Relation method call is relying on method_missing, but if `Kernel` has
the same named method (e.g. `open`, etc), it will invoke Kernel's method
since method_missing is not happened.

To prevent that, eager generate relation methods if a method is the same
name on `Kernel`.

Fixes rails#39195.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants