counter_cache с has_many: через - PullRequest
       2

counter_cache с has_many: через

22 голосов
/ 10 марта 2011

Я только что создал поле 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, и я не знаю, как сказать, что это для ассоциации has_many: through.

  belongs_to :user, :counter_cache => :received_likes

Ответы [ 3 ]

22 голосов
/ 20 марта 2014

У вас есть предыдущий

    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
13 голосов
/ 11 марта 2011

Согласно этой записи (с прошлого месяца) и этой записи (с 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 has_many C: через => B

3 голосов
/ 26 июня 2011

Это, в основном, делает то же самое:

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 для тегов или любые ассоциации, которые у вас есть.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...