tapioca icon indicating copy to clipboard operation
tapioca copied to clipboard

Add hash return type for relation groupchain size

Open marknuzz opened this issue 1 year ago • 0 comments

Motivation

Model.where(...).group(...).size returns a hash, not a scalar number.

Implementation

This is a special case where ActiveRecord::Calculations doesn't handle the size method.

https://github.com/rails/rails/blob/6f57590388ca38ed2b83bc1207a8be13a9ba2aef/activerecord/lib/active_record/relation.rb#L273-L280

# activerecord-7.1.4/lib/active_record/relation.rb
def size
  if loaded?
    records.length
  else
    count(:all)
  end
end
Model.group('created_at').count(:all) == Model.group('created_at').size
=> true

We can handle this by adding a special case for the size method as we can't rely on the Calculations class to provide the name of the method.

Tests

Yes

marknuzz avatar Oct 15 '24 03:10 marknuzz