Merge pull request #4011 from lest/scope-with-lambda-duplicates

call scope within unscoped to prevent duplication of where values
This commit is contained in:
Jon Leighton
2011-12-18 09:35:06 -08:00
3 changed files with 8 additions and 1 deletions

View File

@@ -177,7 +177,7 @@ module ActiveRecord
extension = Module.new(&Proc.new) if block_given?
scope_proc = lambda do |*args|
options = scope_options.respond_to?(:call) ? scope_options.call(*args) : scope_options
options = scope_options.respond_to?(:call) ? unscoped { scope_options.call(*args) } : scope_options
options = scoped.apply_finder_options(options) if options.is_a?(Hash)
relation = scoped.merge(options)

View File

@@ -337,6 +337,11 @@ class NamedScopeTest < ActiveRecord::TestCase
end
end
def test_should_not_duplicates_where_values
where_values = Topic.where("1=1").scope_with_lambda.where_values
assert_equal ["1=1"], where_values
end
def test_chaining_with_duplicate_joins
join = "INNER JOIN comments ON comments.post_id = posts.id"
post = Post.find(1)

View File

@@ -8,6 +8,8 @@ class Topic < ActiveRecord::Base
scope :approved, :conditions => {:approved => true}
scope :rejected, :conditions => {:approved => false}
scope :scope_with_lambda, lambda { scoped }
scope :by_lifo, :conditions => {:author_name => 'lifo'}
scope :approved_as_hash_condition, :conditions => {:topics => {:approved => true}}