Skip to content

Instantly share code, notes, and snippets.

@yoshifumi0521
Created January 2, 2013 16:05
Show Gist options
  • Save yoshifumi0521/4435621 to your computer and use it in GitHub Desktop.
Save yoshifumi0521/4435621 to your computer and use it in GitHub Desktop.
Railsのカウントキャッシュ。1対多関係で、親モデルに、子モデルの数をカウントするための設定。処理スピードがあがる。
#親のArticleモデル
def class Article < ActiveRecord::Base
has_many :comments
end
#子のCommentモデル
class Comment < ActiveRecord::Base
belongs_to :article,:counter_cache => true
end
#Commentモデルに、comments_countを追加。マイグレーションファイル。
t.integer :comments_count,:default=>0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment