diff options
author | Jon Rowe <hello@jonrowe.co.uk> | 2012-03-15 18:27:43 +0000 |
---|---|---|
committer | Jon Rowe <hello@jonrowe.co.uk> | 2012-03-15 19:18:31 +0000 |
commit | f31670d0270342f37846082ac1a27fc6d3400a8a (patch) | |
tree | 060604c109ef1fb91a248f707c6c2b0ecf9e3799 /activerecord | |
parent | fbc9d0f44f090a9873834f4966760c3e80682559 (diff) | |
download | rails-f31670d0270342f37846082ac1a27fc6d3400a8a.tar.gz rails-f31670d0270342f37846082ac1a27fc6d3400a8a.tar.bz2 rails-f31670d0270342f37846082ac1a27fc6d3400a8a.zip |
when using a preloaded array and the uniq flag is set then return the size of the uniq array
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/lib/active_record/associations/collection_association.rb | 8 | ||||
-rw-r--r-- | activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb | 6 |
2 files changed, 12 insertions, 2 deletions
diff --git a/activerecord/lib/active_record/associations/collection_association.rb b/activerecord/lib/active_record/associations/collection_association.rb index da4c311bce..08d53d8c56 100644 --- a/activerecord/lib/active_record/associations/collection_association.rb +++ b/activerecord/lib/active_record/associations/collection_association.rb @@ -248,8 +248,12 @@ module ActiveRecord # This method is abstract in the sense that it relies on # +count_records+, which is a method descendants have to provide. def size - if !find_target? || (loaded? && !options[:uniq]) - target.size + if !find_target? || loaded? + if options[:uniq] + target.uniq.size + else + target.size + end elsif !loaded? && options[:group] load_target.size elsif !loaded? && !options[:uniq] && target.is_a?(Array) diff --git a/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb b/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb index f457dfb9b3..2fa2c6d13f 100644 --- a/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb @@ -338,6 +338,12 @@ class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase assert_equal 3, project.developers.size end + def test_uniq_when_association_already_loaded + project = projects(:active_record) + project.developers << [ developers(:jamis), developers(:david), developers(:jamis), developers(:david) ] + assert_equal 3, Project.includes(:developers).find(project.id).developers.size + end + def test_deleting david = Developer.find(1) active_record = Project.find(1) |