Skip to content

Commit

Permalink
default scope merge where clauses [#5488 state:resolved]
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan authored and tenderlove committed Oct 20, 2010
1 parent 4480521 commit f294540
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
1 change: 1 addition & 0 deletions activerecord/lib/active_record/base.rb
Expand Up @@ -1160,6 +1160,7 @@ def with_exclusive_scope(method_scoping = {}, &block)
# 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

Expand Down
1 change: 1 addition & 0 deletions activerecord/test/cases/base_test.rb
Expand Up @@ -1443,6 +1443,7 @@ def test_default_scope_is_reset
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]
Expand Down
17 changes: 17 additions & 0 deletions activerecord/test/cases/relation_scoping_test.rb
Expand Up @@ -364,6 +364,23 @@ def test_default_scope_called_twice_merges_conditions
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 }
Expand Down

0 comments on commit f294540

Please sign in to comment.