diff options
author | Jon Leighton <j@jonathanleighton.com> | 2013-06-28 13:32:54 +0100 |
---|---|---|
committer | Jon Leighton <j@jonathanleighton.com> | 2013-06-28 13:45:57 +0100 |
commit | 94924dc32baf78f13e289172534c2e71c9c8cade (patch) | |
tree | 3cceaa74a235e9cc71388522fe13ebcc0a7ae228 /activerecord/test/cases/counter_cache_test.rb | |
parent | e66c148571ff86144e49f23c43e5dd686e67da29 (diff) | |
download | rails-94924dc32baf78f13e289172534c2e71c9c8cade.tar.gz rails-94924dc32baf78f13e289172534c2e71c9c8cade.tar.bz2 rails-94924dc32baf78f13e289172534c2e71c9c8cade.zip |
Simplify/fix implementation of default scopes
The previous implementation was necessary in order to support stuff
like:
class Post < ActiveRecord::Base
default_scope where(published: true)
scope :ordered, order("created_at")
end
If we didn't evaluate the default scope at the last possible moment
before sending the SQL to the database, it would become impossible to
do:
Post.unscoped.ordered
This is because the default scope would already be bound up in the
"ordered" scope, and therefore wouldn't be removed by the
"Post.unscoped" part.
In 4.0, we have deprecated all "eager" forms of scopes. So now you must
write:
class Post < ActiveRecord::Base
default_scope { where(published: true) }
scope :ordered, -> { order("created_at") }
end
This prevents the default scope getting bound up inside the "ordered"
scope, which means we can now have a simpler/better/more natural
implementation of default scoping.
A knock on effect is that some things that didn't work properly now do.
For example it was previously impossible to use #except to remove a part
of the default scope, since the default scope was evaluated after the
call to #except.
Diffstat (limited to 'activerecord/test/cases/counter_cache_test.rb')
0 files changed, 0 insertions, 0 deletions