Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When Active Record has already loaded a unique association .size returns the wrong number. #5453

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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