Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Write a more comprehensive CHANGELOG message [ci skip]
- Loading branch information
1 parent
f1082b8
commit 9f007d7
Showing
1 changed file
with
45 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,50 @@ | ||
## Rails 4.0.0 (unreleased) ## | ||
|
||
* Previously `Post.active.inactive` used to result in `Post.inactive` | ||
since the last where clause used to win while combining scopes. | ||
Now all the scopes will be merged using `AND`. Fixes #7365 . | ||
* Change the semantics of combining scopes to be the same as combining | ||
class methods which return scopes. For example: | ||
|
||
class User < ActiveRecord::Base | ||
scope :active, -> { where state: 'active' } | ||
scope :inactive, -> { where state: 'inactive' } | ||
end | ||
|
||
class Post < ActiveRecord::Base | ||
def self.active | ||
where state: 'active' | ||
end | ||
|
||
def self.inactive | ||
where state: 'inactive' | ||
end | ||
end | ||
|
||
### BEFORE ### | ||
|
||
User.where(state: 'active').where(state: 'inactive') | ||
# => SELECT * FROM users WHERE state = 'active' AND state = 'inactive' | ||
|
||
User.active.inactive | ||
# => SELECT * FROM users WHERE state = 'inactive' | ||
|
||
Post.active.inactive | ||
# => SELECT * FROM posts WHERE state = 'active' AND state = 'inactive' | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
jonleighton
Author
Member
|
||
|
||
### AFTER ### | ||
|
||
User.active.inactive | ||
# => SELECT * FROM posts WHERE state = 'active' AND state = 'inactive' | ||
|
||
Before this change, invoking a scope would merge it into the current | ||
scope and return the result. `Relation#merge` applies "last where | ||
wins" logic to de-duplicate the conditions, but this lead to | ||
confusing and inconsistent behaviour. This fixes that. | ||
|
||
If you really do want the "last where wins" logic, you can opt-in to | ||
it like so: | ||
|
||
User.active.merge(User.inactive) | ||
|
||
Fixes #7365. | ||
|
||
*Neeraj Singh* and *Jon Leighton* | ||
|
||
|
3 comments
on commit 9f007d7
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.
❤️
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.
Awesome 💖
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.
Daaaaamn. 😄
Is not this line wrong?