Skip to content
Browse files

Prevent to apply distinct options twice for chained scopes [#728 stat…

…e:resolved]
  • Loading branch information...
1 parent 4f4a47f commit 95bc06a29e04c8b9a83002d7b0767ce34a1e066d Jon de Andres committed with bfcapell Feb 22, 2012
View
2 ubiquo_core/lib/ubiquo/extensions/distinct_option.rb
@@ -22,7 +22,7 @@ def self.included klass
# Applies the :distinct option when constructing sql queries
def apply_finder_options_with_distinct(options)
- if options[:distinct]
+ if options[:distinct] && !self.using_distinct
options_with_distinct = options.merge(:select => select_distinct(options))
self.using_distinct = true
end
View
16 ubiquo_core/test/unit/ubiquo/extensions/distinct_option_test.rb
@@ -20,6 +20,22 @@ def test_distinct_option_enabled_in_scope
assert_equal 1, results.size, ':distinct option does not work as expected in scopes'
end
+ def test_distinct_option_enabled_in_chained_scopes
+ TestOnlyModel.destroy_all
+ TestOnlyModel.create!(:name => "test1")
+ TestOnlyModel.create!(:name => "test2")
+ scope1 = {
+ :distinct => true,
+ :conditions => ["name like ?", "te%"]
+ }
+ scope2 = {
+ :distinct => true,
+ :conditions => ["name like ?", "%test%"]
+ }
+ results = TestOnlyModel.scoped(scope1).scoped(scope2).all
+ assert_equal 2, results.size
+ end
+
def test_distinct_option_does_not_affect_order_and_select
complex_order = 'name DESC, test_only_models.id ASC, test_only_model_twos.title'
%w{all scoped}.each do |method|

0 comments on commit 95bc06a

Please sign in to comment.
Something went wrong with that request. Please try again.