Permalink
Browse files

when using a preloaded array and the uniq flag is set then return the…

… size of the uniq array
  • Loading branch information...
1 parent fbc9d0f commit f31670d0270342f37846082ac1a27fc6d3400a8a @JonRowe JonRowe committed Mar 15, 2012
@@ -248,8 +248,12 @@ def destroy(*records)
# 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)
@@ -338,6 +338,12 @@ def test_uniq_option_prevents_duplicate_push
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)

0 comments on commit f31670d

Please sign in to comment.