• R
    Deprecate aggregations with group by duplicated fields · 2580b83f
    Ryuta Kamizono 提交于
    We've learned that `merge` causes duplicated multiple values easily, so
    if we missed to deduplicate the values, it will cause weird behavior
    like #38052, #39171.
    
    I've investigated the deduplication for the values, at least that had
    existed since Rails 3.0.
    
    https://github.com/rails/rails/commit/bed9179aa1496f6d28891cf515af0d7e515ebbab
    
    Aggregations with group by multiple fields was introduced at Rails 3.1,
    but we missed the deduplication for the aggregation result, unlike the
    generated SQL.
    
    https://github.com/rails/rails/commit/a5cdf0b9eb860c4370ae5fde231e1b61f71b6b65
    
    While the investigation, I've found that `annotate` is also missed the
    deduplication.
    
    I don't suppose this weird behavior is intended for both.
    
    So I'd like to deprecate the duplicated behavior in Rails 6.1, and will
    be deduplicated all multiple values in Rails 6.2.
    
    To migrate to Rails 6.2's behavior, use `uniq!(:group)` to deduplicate
    group fields.
    
    ```ruby
    accounts = Account.group(:firm_id)
    
    # duplicated group fields, deprecated.
    accounts.merge(accounts.where.not(credit_limit: nil)).sum(:credit_limit)
    # => {
    #   [1, 1] => 50,
    #   [2, 2] => 60
    # }
    
    # use `uniq!(:group)` to deduplicate group fields.
    accounts.merge(accounts.where.not(credit_limit: nil)).uniq!(:group).sum(:credit_limit)
    # => {
    #   1 => 50,
    #   2 => 60
    # }
    ```
    2580b83f
query_methods.rb 49.6 KB