counter_cache with has_many :through

前端 未结 3 1202
温柔的废话
温柔的废话 2020-11-30 04:59

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

 @users = User.where(:sex => 2).order(\'received_likes_count\')
<
3条回答
  •  旧时难觅i
    2020-11-30 05:29

    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.

提交回复
热议问题