Skip to content

Commit

Permalink
when using a preloaded array and the uniq flag is set then return the…
Browse files Browse the repository at this point in the history
… size of the uniq array
  • Loading branch information
JonRowe committed Mar 15, 2012
1 parent fbc9d0f commit f31670d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
Expand Up @@ -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)
Expand Down
Expand Up @@ -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)
Expand Down

0 comments on commit f31670d

Please sign in to comment.