mbleigh / acts-as-taggable-on

A tagging plugin for Rails applications that allows for custom tagging along dynamic contexts.
http://mbleigh.lighthouseapp.com/projects/10116-acts-as-taggable-on
MIT License
4.96k stars 1.2k forks source link

Including :tags on a relation and do a group by sum will give a wrong value #1022

Closed github0013 closed 3 years ago

github0013 commented 3 years ago

Environments

Reproduction

# == Schema Information
#
# Table name: products
#
#  id         :bigint           not null, primary key
#  quantity   :integer          not null
#  created_at :datetime         not null
#  updated_at :datetime         not null
#

class Product < ApplicationRecord
  acts_as_taggable
end

Product.destroy_all
Product.create!(quantity: 1)
Product.sum(:quantity) # => 1
Product.includes(:tags).sum(:quantity) # => 1
Product.group(:id).sum(:quantity) # => {1 => 1}
Product.includes(:tags).group(:id).sum(:quantity) # => {1 => 1}

Product.destroy_all
Product.create!(quantity: 1, tag_list: %w[a b c])
Product.sum(:quantity) # => 1
Product.includes(:tags).sum(:quantity) # => 3 !!!!
Product.group(:id).sum(:quantity) # => {2 => 1}
Product.includes(:tags).group(:id).sum(:quantity) # => {2 => 3} !!!!

Expected

Product.destroy_all
Product.create!(quantity: 1, tag_list: %w[a b c])
Product.includes(:tags).sum(:quantity) # => this should give 1

Actual

Product.destroy_all
Product.create!(quantity: 1, tag_list: %w[a b c])
Product.includes(:tags).sum(:quantity) # => 3, instead of 1