diff options
author | Rick Olson <technoweenie@gmail.com> | 2007-07-20 00:10:06 +0000 |
---|---|---|
committer | Rick Olson <technoweenie@gmail.com> | 2007-07-20 00:10:06 +0000 |
commit | 3029a76af32113676182f902ad4f91d82c2734de (patch) | |
tree | a8d06bbeeccef27dade4955a72c901a100781add /activerecord | |
parent | 9014bf3f262a6b55aa4a05e2626aeebf688aa05a (diff) | |
download | rails-3029a76af32113676182f902ad4f91d82c2734de.tar.gz rails-3029a76af32113676182f902ad4f91d82c2734de.tar.bz2 rails-3029a76af32113676182f902ad4f91d82c2734de.zip |
Fix #count on a has_many :through association so that it recognizes the :uniq option. Closes #8801 [lifofifo]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7199 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/CHANGELOG | 2 | ||||
-rw-r--r-- | activerecord/lib/active_record/associations/has_many_through_association.rb | 10 | ||||
-rw-r--r-- | activerecord/test/associations/join_model_test.rb | 11 |
3 files changed, 22 insertions, 1 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index 7a32ac5e44..886d049de0 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Fix #count on a has_many :through association so that it recognizes the :uniq option. Closes #8801 [lifofifo] + * Fix and properly document/test count(column_name) usage. Closes #8999 [lifofifo] * Remove deprecated count(conditions=nil, joins=nil) usage. Closes #8993 [lifofifo] diff --git a/activerecord/lib/active_record/associations/has_many_through_association.rb b/activerecord/lib/active_record/associations/has_many_through_association.rb index b75de4d23f..3d60da76b3 100644 --- a/activerecord/lib/active_record/associations/has_many_through_association.rb +++ b/activerecord/lib/active_record/associations/has_many_through_association.rb @@ -100,6 +100,16 @@ module ActiveRecord def sum(*args, &block) calculate(:sum, *args, &block) end + + def count(*args) + column_name, options = @reflection.klass.send(:construct_count_options_from_args, *args) + if @reflection.options[:uniq] + # This is needed becase 'SELECT count(DISTINCT *)..' is not valid sql statement. + column_name = "#{@reflection.klass.table_name}.#{@reflection.klass.primary_key}" if column_name == :all + options.merge!(:distinct => true) + end + @reflection.klass.send(:with_scope, construct_scope) { @reflection.klass.count(column_name, options) } + end protected def method_missing(method, *args, &block) diff --git a/activerecord/test/associations/join_model_test.rb b/activerecord/test/associations/join_model_test.rb index a784a8eefd..a24763b92e 100644 --- a/activerecord/test/associations/join_model_test.rb +++ b/activerecord/test/associations/join_model_test.rb @@ -30,7 +30,16 @@ class AssociationsJoinModelTest < Test::Unit::TestCase assert_equal 2, authors(:mary).categorized_posts.size assert_equal 1, authors(:mary).unique_categorized_posts.size end - + + def test_has_many_uniq_through_count + author = authors(:mary) + assert !authors(:mary).unique_categorized_posts.loaded? + assert_queries(1) { assert_equal 1, author.unique_categorized_posts.count } + assert_queries(1) { assert_equal 1, author.unique_categorized_posts.count(:title) } + assert_queries(1) { assert_equal 0, author.unique_categorized_posts.count(:title, :conditions => "title is NULL") } + assert !authors(:mary).unique_categorized_posts.loaded? + end + def test_polymorphic_has_many assert posts(:welcome).taggings.include?(taggings(:welcome_general)) end |