0

레일 3.1.4 (루비 1.8.7)에서 카운터 캐시 메커니즘에 이상한 문제가 있습니다. 개발 서버에서 앱을 실행하면 모든 것이 잘 작동합니다. 프로덕션 환경으로 전환 한 후 comments_count 열은 하나의 주석을 추가 할 때마다 +2 씩 증가합니다.레일 카운터 캐시가 작동하지 않습니다.

나는 로그를 검토 한 증가 쿼리가 두 번이 나타납니다 :이 하루 종일 해결하기 위해 노력하고

Started POST "/comments" for 127.0.0.1 at Mon May 21 16:39:49 +0200 2012 
    Processing by CommentsController#create as HTML 
    Parameters: {"comment"=>{"content_id"=>"2803", "content_type"=>"Post", "value"=>"hello there"}, "authenticity_token"=>"/2HRoMq/I+fPzLlKB8CezWEpwjywHo5RwCgm2VRj0zo=", "utf8"=>"✓", "commit"=>"Dodaj"} 
    [1m[36mUser Load (0.4ms)[0m [1mSELECT `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1[0m 
    1 row, 646 bajty 
    app/controllers/application_controller.rb:27:in `current_user_session' 
    app/controllers/application_controller.rb:32:in `current_user' 
    app/controllers/application_controller.rb:41:in `require_user' 
    [1m[35m (0.2ms)[0m BEGIN 
    [1m[36m (1.0ms)[0m [1mUPDATE `users` SET `last_request_at` = '2012-05-21 14:39:50', `updated_at` = '2012-05-21 14:39:50', `perishable_token` = 'U6r38z0lX1G7Y48xC9s4' WHERE `users`.`id` = 1[0m 
    [1m[35m (46.0ms)[0m COMMIT 
    [1m[36m (0.2ms)[0m [1mBEGIN[0m 
    [1m[35mBlacklist Load (0.7ms)[0m SELECT `blacklists`.* FROM `blacklists` WHERE (user_id = 1 AND end_date > '2012-05-21 14:39:50') 
    0 rows, 0 bajty 
    app/models/comment.rb:97:in `block_check' 
    app/controllers/comments_controller.rb:25:in `create' 
    [1m[36mSQL (0.6ms)[0m [1mINSERT INTO `comments` (`comments_count`, `content_id`, `content_type`, `created_at`, `edits`, `points`, `reported`, `updated_at`, `user_id`, `value`) VALUES (0, 2803, 'Post', '2012-05-21 14:39:50', 0, 0, NULL, '2012-05-21 14:39:50', 1, 'hello there')[0m 
    [1m[35mPost Load (0.6ms)[0m SELECT `posts`.* FROM `posts` WHERE `posts`.`id` = 2803 LIMIT 1 
    1 row, 658 bajty 
    app/controllers/comments_controller.rb:25:in `create' 
    [1m[36mSQL (0.6ms)[0m [1mUPDATE `posts` SET `comments_count` = COALESCE(`comments_count`, 0) + 1 WHERE `posts`.`id` = 2803[0m 
    [1m[35mSQL (0.4ms)[0m UPDATE `posts` SET `comments_count` = COALESCE(`comments_count`, 0) + 1 WHERE `posts`.`id` = 2803 
    [1m[36m (39.2ms)[0m [1mCOMMIT[0m 
    Redirected to http://0.0.0.0:3000/news/zobacz/Tibia-obt-startuje-w-listopadzie-2 

내가 잘못 무엇인지 전혀 모른다. 개발 모드에서는 모든 것이 정상적으로 작동합니다. Rails2에서 업그레이드 한 후에 문제가 나타났습니다.

+0

이 정보가 도움이 되나요? http://stackoverflow.com/questions/9476054/activerecord-has-many-through-duplicating-counter-caches-on-mass-assignment –

+0

나는 두렵다. :( – Arti

답변

0

문제가 해결되었습니다. 내가 말했듯이, 나는 Rails2에서 마이그레이션 한 내가되지 않는 문 다음 내 모델에 있었다 :

belongs_to :content, :polymorphic => true, :counter_cache => true 

Rails3 자동 counter_cache 필드를 처리하고 문 위에 중복됩니다. 내 모델 관계를 다음과 같이 변경했습니다.

belongs_to :content, :polymorphic => true 

매력처럼 작동했습니다.