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

Rewhere on a has_many association drops WHERE clause in 4.2.4 #21955

Closed
scottohara opened this issue Oct 13, 2015 · 2 comments
Closed

Rewhere on a has_many association drops WHERE clause in 4.2.4 #21955

scottohara opened this issue Oct 13, 2015 · 2 comments

Comments

@scottohara
Copy link

In 4.2.4, it appears that using a rewhere scope on a has_many relation is broken (or at least, has materially changed from earlier versions).

The following gists demonstrate the issue:

activerecord 4.2.3 ('working' example)

https://gist.github.com/scottohara/08d73351e27adeb6f694

In this example, a rewhere scope on the has_many :comments association arbitrarily overrides the WHERE clause to a different id (ie. -> { rewhere(post_id: 99) }).

The SQL SELECT statement for post.comments is as expected:

SELECT "comments".* FROM "comments" WHERE "comments"."post_id" = 99

activerecord 4.2.4 ('broken' example)

https://gist.github.com/scottohara/70a07172882e067aaa96

This is identical to the above gist, but with the activerecord gem version bumped to 4.2.4.

The presence of the rewhere scope now causes the SQL SELECT statement to lose its WHERE clause entirely; yielding:

SELECT "comments".* FROM "comments"

The same behaviour (as 4.2.4) is present in edge (master).

Is this a regression in 4.2.4, or an expected behaviour change? (and if expected, what would be the correct way to use rewhere in 4.2.4?)

@jbranchaud
Copy link
Contributor

It looks like rewhere uses unscope(...).where(...) under the hood. I think the where constraints are not being applied after the unscoping.

jbranchaud added a commit to jbranchaud/rails that referenced this issue Oct 19, 2015
With `unscope!` called last, it undoes `where` constraints of the same
value when the `where` is chained after the `unscope`. This is what a
`rewhere` does. This is undesirable behavior.

The included tests demonstrate both the `unscope(...).where(...)`
behavior as well as the direct use of `rewhere(...)`.

This is in reference to rails#21955.
rafaelfranca added a commit that referenced this issue Oct 20, 2015
@rafaelfranca
Copy link
Member

Fixed in 2ba92bb and backported in 1cfcf41

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

No branches or pull requests

3 participants