def hack_for_has_many_counter_cache(parent_record, column, value)
      association = parent_record.association(column.name)
      counter_attr = association.send(:cached_counter_attribute_name)
      difference = value.select(&:persisted?).size - parent_record.send(counter_attr)

      if parent_record.new_record?
        parent_record.send "#{column.name}=", value
        parent_record.send "#{counter_attr}_will_change!"
      else
        # don't decrement counter for deleted records, on destroy they will update counter
        difference += (parent_record.send(column.name) - value).size
        association.send :update_counter, difference unless difference.zero?
      end

      # update counters on old parents if belongs_to is changed
      value.select(&:persisted?).each do |record|
        key = record.send(column.association.foreign_key)
        parent_record.class.decrement_counter counter_attr, key if key && key != parent_record.id # rubocop:disable Rails/SkipsModelValidations
      end
      parent_record.send "#{column.name}=", value if parent_record.persisted?
    end
def hack_for_has_many_counter_cache(parent_record, column, value)
      association = parent_record.association(column.name)
      counter_attr = association.send(:cached_counter_attribute_name)
      difference = value.select(&:persisted?).size - parent_record.send(counter_attr)

      if parent_record.new_record?
        parent_record.send "#{column.name}=", value
        parent_record.send "#{counter_attr}_will_change!"
      else
        # don't decrement counter for deleted records, on destroy they will update counter
        difference += (parent_record.send(column.name) - value).size
        association.send :update_counter, difference unless difference.zero?
      end

      # update counters on old parents if belongs_to is changed
      value.select(&:persisted?).each do |record|
        key = record.send(column.association.foreign_key)
        parent_record.class.decrement_counter counter_attr, key if key && key != parent_record.id # rubocop:disable Rails/SkipsModelValidations
      end
      parent_record.send "#{column.name}=", value if parent_record.persisted?
    end
workaround to update counters when polymorphic has_many changes on persisted record
 TODO: remove when rails4 support is removed or counter cache for polymorphic has_many association works on rails4