0

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 升级后出现问题。

4

1 回答 1

0

问题解决了。正如我所说,我已经从 Rails2 迁移,并且在我的模型中遵循了已弃用的声明:

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

Rails3 自动处理 counter_cache 字段,上面的语句是多余的。我将模型关系更改为:

belongs_to :content, :polymorphic => true

像魅力一样工作。

于 2012-05-22T15:33:59.727 回答