I've got weird problem with counter cache mechanism at Rails 3.1.4 (ruby 1.8.7). When I am running my app on development server everything works great. After switching to production environment comments_count column increments +2 each time I am adding just one comment.
I've looked through logs and increment query shows up there twice:
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
I am trying to fix this whole day and I have no idea what is wrong. In development mode everything works OK. Problems appeared after upgrade from Rails2.
Problem solved. As I said, I've migrated from Rails2 and I had at my model following deprecated statement:
Rails3 automatically handles counter_cache fields and above statement is redundant. I changed my model relation to:
Worked like a charm.