尝试将 counter_cache 添加到自连接 rails 模型时出错

Error when trying to add counter_cache to a self-join rails model

class Product < ActiveRecord::Base 
    belongs_to :parent, :class_name => 'Product', :foreign_key => :parent_id
    has_many :children, :class_name => 'Product', :foreign_key => :parent_id

我正在尝试将计数器缓存添加到 :children 列。我尝试了以下方法:

belongs_to :parent, :class_name => 'Product', :foreign_key => :parent_id
has_many :children, :class_name => 'Product', :foreign_key => :parent_id, counter_cache: true

还有:

has_many :children, :class_name => 'Product', :foreign_key => :parent_id, counter_cache: :children_count

当我运行Product.reset_counters(foo.id, :children)

我收到以下错误:

NoMethodError: undefined method `counter_cache_column' for nil:NilClass

我是不是不了解 counter_cache 或自连接的一些基本知识?相关信息很少,不适用于此类自连接。

柜台现金应该在属于喜欢

class Child  < ActiveRecord::Base
  belongs_to :product, counter_cache: true
...

不上有很多

class Product < ActiveRecord::Base
  belongs_to :parent, :class_name => 'Product', :foreign_key => :parent_id
  has_many :children, :class_name => 'Product', :foreign_key => :parent_id

但数据库列应该仍然在产品上

在此 link 通读 4.1.2.3 了解更多信息