diff options
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/lib/active_record/base.rb | 1 | ||||
-rw-r--r-- | activerecord/test/cases/base_test.rb | 1 | ||||
-rw-r--r-- | activerecord/test/cases/relation_scoping_test.rb | 17 |
3 files changed, 19 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 6720f0687a..0e41a1c35c 100644 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -1139,6 +1139,7 @@ MSG # Article.new.published # => true # Article.create.published # => true def default_scope(options = {}) + reset_scoped_methods self.default_scoping << construct_finder_arel(options, default_scoping.pop) end diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb index 8acee9ac71..21bd61c096 100644 --- a/activerecord/test/cases/base_test.rb +++ b/activerecord/test/cases/base_test.rb @@ -1456,6 +1456,7 @@ class BasicsTest < ActiveRecord::TestCase UnloadablePost.class_eval do default_scope order('posts.comments_count ASC') end + UnloadablePost.scoped_methods # make Thread.current[:UnloadablePost_scoped_methods] not nil UnloadablePost.unloadable assert_not_nil Thread.current[:UnloadablePost_scoped_methods] diff --git a/activerecord/test/cases/relation_scoping_test.rb b/activerecord/test/cases/relation_scoping_test.rb index 689cce8746..a27e2e72cd 100644 --- a/activerecord/test/cases/relation_scoping_test.rb +++ b/activerecord/test/cases/relation_scoping_test.rb @@ -393,6 +393,23 @@ class DefaultScopingTest < ActiveRecord::TestCase assert_equal 100000, klass.first.salary end + def test_default_scope_called_twice_in_different_place_merges_where_clause + Developer.destroy_all + Developer.create!(:name => "David", :salary => 80000) + Developer.create!(:name => "David", :salary => 100000) + Developer.create!(:name => "Brian", :salary => 100000) + + klass = Class.new(Developer) + klass.class_eval do + default_scope where("name = 'David'") + default_scope where("salary = 100000") + end + + assert_equal 1, klass.count + assert_equal "David", klass.first.name + assert_equal 100000, klass.first.salary + end + def test_method_scope expected = Developer.find(:all, :order => 'salary DESC, name DESC').collect { |dev| dev.salary } received = DeveloperOrderedBySalary.all_ordered_by_name.collect { |dev| dev.salary } |