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/scoping | |
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/scoping')
-rw-r--r-- | activerecord/test/cases/scoping/default_scoping_test.rb | 5 |
1 files changed, 2 insertions, 3 deletions
diff --git a/activerecord/test/cases/scoping/default_scoping_test.rb b/activerecord/test/cases/scoping/default_scoping_test.rb index 0f69443839..5c7751b445 100644 --- a/activerecord/test/cases/scoping/default_scoping_test.rb +++ b/activerecord/test/cases/scoping/default_scoping_test.rb @@ -153,9 +153,8 @@ class DefaultScopingTest < ActiveRecord::TestCase end def test_order_to_unscope_reordering - expected = DeveloperOrderedBySalary.all.collect { |dev| [dev.name, dev.id] } - received = DeveloperOrderedBySalary.order('salary DESC, name ASC').reverse_order.unscope(:order).collect { |dev| [dev.name, dev.id] } - assert_equal expected, received + scope = DeveloperOrderedBySalary.order('salary DESC, name ASC').reverse_order.unscope(:order) + assert !(scope.to_sql =~ /order/i) end def test_unscope_reverse_order |