3

我有两个模型我使用多态 has_many 通过关联链接在一起,我想添加一个 counter_cache 但似乎 Rails/ActiveRecord 不支持此功能开箱即用。

class Classifiable < ActiveRecord::Base
  has_many :classifications, :as => :classifiable, :foreign_key => :classifiable_id
end

class Taxonomy < ActiveRecord::Base
  has_many :classifications, :as => :taxonomy, :foreign_key => :taxonomy_id
end

class Question < Classifiable
  has_many :categories, :through => :classifications, :as => :classifiable, :source => :taxonomy, :source_type => "Category"
end

class Category < Taxonomy
  has_many :questions, :through => :classifications, :source => :classifiable, :source_type => "Question"
end

class Classification < ActiveRecord::Base
  attr_accessible :classifiable, :classifiable_id, :classifiable_type,
                  :taxonomy, :taxonomy_id, :taxonomy_type

  belongs_to :classifiable, :polymorphic => true
  belongs_to :taxonomy,     :polymorphic => true
end
4

3 回答 3

10

只需修改以下分类模型:

class Classification < ActiveRecord::Base
  attr_accessible :classifiable, :classifiable_id, :classifiable_type,
                  :taxonomy, :taxonomy_id, :taxonomy_type

  belongs_to :classifiable, :polymorphic => true
  belongs_to :taxonomy,     :polymorphic => true

  before_create  :increment_counter
  before_destroy :decrement_counter

  private

  # increments the right classifiable counter for the right taxonomy
  def increment_counter
    self.taxonomy_type.constantize.increment_counter("#{self.classifiable_type.downcase.pluralize}_count", self.taxonomy_id)
  end

  # decrements the right classifiable counter for the right taxonomy
  def decrement_counter
    self.taxonomy_type.constantize.decrement_counter("#{self.classifiable_type.downcase.pluralize}_count", self.taxonomy_id)
  end
end

此外,请确保您的分类表中有以下列:

t.integer :questions_count,           :null => false, :default => 0
t.integer :other_classifiables_count, :null => false, :default => 0
t.integer :other_classifiables_count, :null => false, :default => 0
t.integer :other_classifiables_count, :null => false, :default => 0

将“other_classifiables_count”更改为您需要的(“answers_count”、“users_count”等)

于 2013-03-31T01:25:18.973 回答
3

在调用 delete 时,Rails 似乎没有经过 before/after_destroy 回调(当您删除 a 时会发生很多通过关联)。

相反,您可以使用关联的回调#before_add#before_remove

class Question < Classifiable
  has_many :categories, through: :classifications, 
                        as: :classifiable, 
                        source: :taxonomy, 
                        source_type: Category,
                        before_add: :increment_counter

  def increment_counter(category)
    # increment counter, etc.
  end

end
于 2014-07-09T21:23:48.757 回答
2

要稍微修改乔纳森的答案,您可以让它在增加/减少之前查找列类型以查看它是否存在。我也把它弄干了一点:

def increment_counter(direction=:increment)
  ar_class  = self.taxonomy_type.constantize
  ar_column = "#{self.taxonomy_type.underscore.pluralize}_count"

  if ar_class.columns.include? ar_column
    ar_class.send "#{direction}_counter", ar_column, self.taxonomy_id
  end
end

def decrement_counter
  increment_counter :decrement
end

哦,它适用于MultiWordClassNames. underscoreadowncase所以我的版本省略了它。

于 2014-06-28T22:34:30.540 回答