카운터 캐시는 업데이트되지 않지만 부모와 자녀에게 저장할 수 있습니다.
-
05-09-2019 - |
문제
카운터 캐시를 추가했지만 업데이트 할 수는 없습니다. 그러나 새로운 블로그 게시물을 추가하여 부모 (블로그 게시물 모델)를 업데이트 할 수 있으며 새 댓글을 추가하여 자식을 업데이트 할 수 있습니다. 카운터 캐시는 blog_posts.comments_count 필드를 자동 업데이트하여 블로그 게시물 당 총 주석 수를 추적해야합니다. 나는 내가 겪은 몇 가지 단계를 간략하게 설명하고 누군가가 내가 잘못한 것을 발견하기를 바랍니다. 스키마 덤프가 끝났습니다.
블로그 게시물 모델이 있습니다.
class Post < ActiveRecord::Base
set_table_name("blog_posts")
belongs_to :author, :class_name => "User", :foreign_key => 'author_id'
has_many :comments, :class_name => "Comment",
:foreign_key => 'post_id', :order => "created_at desc", :dependent => :destroy
has_many :categorizations
has_many :categories, :through => :categorizations
named_scope :recent, :order => "created_at desc", :limit => 5
end
그리고 counter_cache가 게시물 모델로 설정된 주석 모델 :
class Comment < ActiveRecord::Base
belongs_to :post, :class_name => "Post", :foreign_key => "post_id", :counter_cache => true
belongs_to :author, :class_name => "User", :foreign_key => "author_id"
end
blog_posts 테이블에 counter_cache 열을 추가하기 위해 마이그레이션을 만들었습니다.
class AddCommentCounter < ActiveRecord::Migration
def self.up
add_column :blog_posts, :comments_count, :integer, :limit => 4, :default => 0, :null => false
Post.find(:all).each do |post|
current_count = post.comments.size
post.update_attribute(:comments_count, current_count)
end
end
def self.down
remove_column :blog_posts, :comments_count
end
end
그러나 마이그레이션은 Current_Count로 블로그 게시물을 업데이트하지 않습니다. 항상 0입니다.
Rails 콘솔을 열어 수동으로 update_attribute를 시도했습니다.
Loading development environment (Rails 2.3.2)
>> p = Post.find 1
p = Post.find 1
=> #<Post id: 1, title: "test", content: "test", author_id: 1, status: "ok", created_at: "2009-05-21 19:27:14", updated_at: "2009-05-24 07:02:35", comments_count: 0>
>> p.comments
p.comments
=> [#<Comment id: 5, post_id: 1, author_id: 1, content: "Fifth Comment", status: "ok", created_at: "2009-05-24 07:08:56", updated_at: "2009-05-24 07:08:56">, #<Comment id: 4, post_id: 1, author_id: 1, content: "Fourth Comment", status: "ok", created_at: "2009-05-24 07:05:32", updated_at: "2009-05-24 07:05:32">, #<Comment id: 3, post_id: 1, author_id: 1, content: "Third Comment", status: "ok", created_at: "2009-05-24 06:34:59", updated_at: "2009-05-24 06:34:59">, #<Comment id: 2, post_id: 1, author_id: 1, content: "Second Comment", status: "ok", created_at: "2009-05-24 05:20:43", updated_at: "2009-05-24 05:20:43">, #<Comment id: 1, post_id: 1, author_id: 1, content: "First Comment", status: "ok", created_at: "2009-05-21 19:27:14", updated_at: "2009-05-21 19:27:14">]
>> p.comments.size
p.comments.size
=> 5
>> p.comments_count
p.comments_count
=> 0
>> p.update_attribute(:comments_count, 5)
p.update_attribute(:comments_count, 5)
=> true
>> p.comments_count
p.comments_count
=> 5
>> p.save
p.save
=> true
그러나 데이터베이스를 볼 때 댓글 _count = 0.
어떤 아이디어라도 즐겁게 감사 할 것입니다.
내 schema.db는 다음과 같습니다.
ActiveRecord::Schema.define(:version => 20090524055907) do
create_table "blog_posts", :force => true do |t|
t.string "title", :limit => 100, :default => "", :null => false
t.text "content", :null => false
t.integer "author_id", :default => 0, :null => false
t.string "status", :limit => 20, :default => "", :null => false
t.datetime "created_at"
t.datetime "updated_at"
t.integer "comments_count", :default => 0, :null => false
end
add_index "blog_posts", ["author_id"], :name => "index_blog_posts_on_author_id"
create_table "categories", :force => true do |t|
t.string "name", :limit => 50, :default => "", :null => false
t.string "short_name", :limit => 30, :default => "", :null => false
t.string "description", :default => "", :null => false
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "categories_posts", :force => true do |t|
t.integer "category_id", :null => false
t.integer "post_id", :null => false
end
add_index "categories_posts", ["category_id"], :name => "index_categories_posts_on_category_id"
add_index "categories_posts", ["post_id"], :name => "index_categories_posts_on_post_id"
create_table "comments", :force => true do |t|
t.integer "post_id", :default => 0, :null => false
t.integer "author_id", :default => 0, :null => false
t.text "content", :null => false
t.string "status", :limit => 25, :default => "", :null => false
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "sessions", :force => true do |t|
t.string "session_id", :default => "", :null => false
t.text "data"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "sessions", ["session_id"], :name => "index_sessions_on_session_id"
add_index "sessions", ["updated_at"], :name => "index_sessions_on_updated_at"
create_table "users", :force => true do |t|
t.string "username", :limit => 25, :default => "", :null => false
t.string "hashed_password", :limit => 40, :default => "", :null => false
t.string "first_name", :limit => 25, :default => "", :null => false
t.string "last_name", :limit => 40, :default => "", :null => false
t.string "email", :limit => 50, :default => "", :null => false
t.string "display_name", :limit => 25, :default => "", :null => false
t.integer "user_level", :limit => 3, :default => 0, :null => false
t.datetime "created_at"
t.datetime "updated_at"
end
end
해결책
업데이트 된 코드를 살펴보십시오 RailScasts 에피소드 23 Counter_Cache에서.
카운터 속성이 있습니다 attr_readonly
. 아마도 업데이트 카운터 마이그레이션에서 업데이트 속성 대신에 사용해야 할 사항은 무엇입니까?
제휴하지 않습니다 StackOverflow