|
1 | 1 | ## Rails 4.0.0 (unreleased) ##
|
2 | 2 |
|
3 |
| -* Previously `Post.active.inactive` used to result in `Post.inactive` |
4 |
| - since the last where clause used to win while combining scopes. |
5 |
| - Now all the scopes will be merged using `AND`. Fixes #7365 . |
| 3 | +* Change the semantics of combining scopes to be the same as combining |
| 4 | + class methods which return scopes. For example: |
| 5 | + |
| 6 | + class User < ActiveRecord::Base |
| 7 | + scope :active, -> { where state: 'active' } |
| 8 | + scope :inactive, -> { where state: 'inactive' } |
| 9 | + end |
| 10 | + |
| 11 | + class Post < ActiveRecord::Base |
| 12 | + def self.active |
| 13 | + where state: 'active' |
| 14 | + end |
| 15 | + |
| 16 | + def self.inactive |
| 17 | + where state: 'inactive' |
| 18 | + end |
| 19 | + end |
| 20 | + |
| 21 | + ### BEFORE ### |
| 22 | + |
| 23 | + User.where(state: 'active').where(state: 'inactive') |
| 24 | + # => SELECT * FROM users WHERE state = 'active' AND state = 'inactive' |
| 25 | + |
| 26 | + User.active.inactive |
| 27 | + # => SELECT * FROM users WHERE state = 'inactive' |
| 28 | + |
| 29 | + Post.active.inactive |
| 30 | + # => SELECT * FROM posts WHERE state = 'active' AND state = 'inactive' |
| 31 | + |
| 32 | + ### AFTER ### |
| 33 | + |
| 34 | + User.active.inactive |
| 35 | + # => SELECT * FROM posts WHERE state = 'active' AND state = 'inactive' |
| 36 | + |
| 37 | + Before this change, invoking a scope would merge it into the current |
| 38 | + scope and return the result. `Relation#merge` applies "last where |
| 39 | + wins" logic to de-duplicate the conditions, but this lead to |
| 40 | + confusing and inconsistent behaviour. This fixes that. |
| 41 | + |
| 42 | + If you really do want the "last where wins" logic, you can opt-in to |
| 43 | + it like so: |
| 44 | + |
| 45 | + User.active.merge(User.inactive) |
| 46 | + |
| 47 | + Fixes #7365. |
6 | 48 |
|
7 | 49 | *Neeraj Singh* and *Jon Leighton*
|
8 | 50 |
|
|
0 commit comments