我在Rails 3.1.4(ruby 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 :(得分:0)
问题解决了。正如我所说的,我已经从Rails2迁移了,并且在我的模型中遵循了弃用语句:
belongs_to :content, :polymorphic => true, :counter_cache => true
Rails3自动处理counter_cache字段,上面的语句是多余的。我将模型关系改为:
belongs_to :content, :polymorphic => true
像魅力一样工作。