Skip to content

Commit 04cea56

Browse files
committed
call scope within unscoped to prevent duplication of where values
1 parent 1bfed9e commit 04cea56

File tree

3 files changed

+8
-1
lines changed

3 files changed

+8
-1
lines changed

activerecord/lib/active_record/scoping/named.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ def scope(name, scope_options = {})
177177
extension = Module.new(&Proc.new) if block_given?
178178

179179
scope_proc = lambda do |*args|
180-
options = scope_options.respond_to?(:call) ? scope_options.call(*args) : scope_options
180+
options = scope_options.respond_to?(:call) ? unscoped { scope_options.call(*args) } : scope_options
181181
options = scoped.apply_finder_options(options) if options.is_a?(Hash)
182182

183183
relation = scoped.merge(options)

activerecord/test/cases/named_scope_test.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,11 @@ def test_size_should_use_length_when_results_are_loaded
337337
end
338338
end
339339

340+
def test_should_not_duplicates_where_values
341+
where_values = Topic.where("1=1").scope_with_lambda.where_values
342+
assert_equal ["1=1"], where_values
343+
end
344+
340345
def test_chaining_with_duplicate_joins
341346
join = "INNER JOIN comments ON comments.post_id = posts.id"
342347
post = Post.find(1)

activerecord/test/models/topic.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ class Topic < ActiveRecord::Base
88
scope :approved, :conditions => {:approved => true}
99
scope :rejected, :conditions => {:approved => false}
1010

11+
scope :scope_with_lambda, lambda { scoped }
12+
1113
scope :by_lifo, :conditions => {:author_name => 'lifo'}
1214

1315
scope :approved_as_hash_condition, :conditions => {:topics => {:approved => true}}

0 commit comments

Comments
 (0)