使用has_many :through关联时的counter_cache

25

我刚刚创建了一个counter_cache字段,控制器的代码如下。

 @users = User.where(:sex => 2).order('received_likes_count')

在 User.rb 中的关联是

 has_many :received_likes, :through => :attachments, :source => :likes, :dependent => :destroy

问题在于counter_cache是在Like.rb的belong_to中声明的,我不知道该如何告诉它这是针对has_many: through关联的。

  belongs_to :user, :counter_cache => :received_likes
3个回答

27

您之前具有的

    class Product
      has_and_belongs_to_many :categories
    end

    class Category
      has_and_belongs_to_many :products
    end

以及迁移

    class CreateCategoriesProducts < ActiveRecord::Migration
      def change
        create_table :categories_products, id: false do |t|
          t.references :category
          t.references :product
        end

        add_index :categories_products, [:category_id, :product_id]
      end
    end

现在将所有内容更改为:
    class Product
      has_many :categories_products, dependent: :destroy
      has_many :categories, through: :categories_products
    end

    class Category
      has_many :categories_products, dependent: :destroy
      has_many :products, through: :categories_products
    end

以及新的内容

    class CategoriesProduct < ActiveRecord::Base
      # this model uses table "categories_products" as it is
      # column products_count is in the table "categories"
      belongs_to :category, counter_cache: :products_count
      belongs_to :product
    end

2
谢谢,适用于Rails 4.2。为了正确处理产品删除,需要在两个“has_many:categories_products”语句中添加“dependent::destroy”选项。 - Tatiana Tyu
这应该是答案。 - Ryan-Neal Mes
与Rails 4.2兼容,按预期工作。 - Thomas Klemm
@TatianaTyu Rails 4.1 上一致的。 - qarol
这是正确的答案。注意表示连接表的模型的复数形式。那可能会让你犯错。CategoriesProduct--先复数再单数。 - mindtonic
显示剩余2条评论

13

根据最近的这篇文章2008年的这篇文章,似乎不可能。然而,后一篇文章提供了一个解决方法的代码(从该链接复制粘贴以方便参考,感谢第二个链接中的DEfusion)。

class C < ActiveRecord::Base
    belongs_to :B

    after_create :increment_A_counter_cache
    after_destroy :decrement_A_counter_cache

    private

    def increment_A_counter_cache
        A.increment_counter( 'c_count', self.B.A.id )
    end

    def decrement_A_counter_cache
        A.decrement_counter( 'c_count', self.B.A.id )
    end
end

(这是用于一个方案,其中C属于B,B属于A,A通过B :through => C拥有多个C)


2
Aivils提供的解决方案看起来比添加钩子更符合Rails的正确/传统解决方案。 - jakeonrails

3

这基本上做了同样的事情:

after_save :cache_post_count_on_tags

def cache_post_count_on_tags
  tags.each {|t| tag.update_attribute(:posts_count, t.posts.size)}
end

你需要在标签上添加一个posts_count列,或者你有其他的关联。

4
我认为您的代码不会递减计数器,因为我认为在 destroy() 方法后并不会调用 after_save 方法。 - pixelearth

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接