25

I just created a counter_cache field and the controller looks like this.

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

The association in User.rb is

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

Problem is that counter_cache is declared in the belong_to of Like.rb and I don't know how to tell it that is for the has_many :through association.

  belongs_to :user, :counter_cache => :received_likes
Martin
  • 11,216
  • 23
  • 83
  • 140

3 Answers3

27

You have previous

    class Product
      has_and_belongs_to_many :categories
    end

    class Category
      has_and_belongs_to_many :products
    end

and migration

    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

now change all to

    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

and new one

    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
Aivils Štoss
  • 858
  • 11
  • 9
  • In other words, when you have a `has_many :through` relationship, you define any and all `counter_cache`'s on the `:through` model. You could complement the first `counter_cache: :products_count` by extending the second relationship: `belongs_to :product, counter_cache: :categories_count` – Frank Koehl Apr 24 '14 at 19:30
  • 2
    Thanks, works on Rails 4.2. To correctly process product deletion, it is needed to add "dependent: :destroy" option to both "has_many :categories_products" statements. – Tatiana Tyu Jul 05 '14 at 21:22
  • Works as expected with Rails 4.2. – Thomas Klemm Dec 03 '15 at 11:41
  • @TatianaTyu The same on Rails 4.1 – qarol Mar 20 '16 at 01:27
  • @Martin I know it's been 5 years and all, but you should mark this as the answer :) – Archonic Jan 18 '17 at 16:47
  • This is the correct answer. Pay attention to the pluralization of the model representing the join table. That can trip you up. `CategoriesProduct` -- plural then singular. – mindtonic Jul 18 '18 at 22:53
13

According to this post (from last month) and this post (from 2008), it doesn't seem to be possible. However, the latter post does have code for a workaround (copy/paste'd from that link for your convenience, credit goes to DEfusion in the second link)

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

(This is for a scheme where C belongs_to B, B belongs_to A, A has_many C :through => B

Community
  • 1
  • 1
Eric Hu
  • 18,048
  • 9
  • 51
  • 67
3

This basically does the same thing:

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

And you need a posts_count column on tags, or whatever associations you have.

thenengah
  • 42,557
  • 33
  • 113
  • 157
  • 4
    Actually I think your code won't decrement the counter because after_save I think isn't called after destroy() – pixelearth Sep 04 '11 at 15:42