Counter_cache - Wielka zagadka

Jakiś czas temu migrowałem z rails2 na rails3 i do tej pory nie uporałem się z paroma dziwnymi sprawami. Najdziwniejsza rzecz z jaką się póki co spotkałem to działanie counter cache na polimorficznym modelu Comments. Gdy odpalam serwer w wersji produkcyjnej i dodaje komentarz przez formularz to pole comments_count nie rośnie o 1 tylko 2. Gdy usuwam komentarz przez kontroler (z poziomu aplikacji) odejmuje się liczba dwa. Na serwerze odpalonym w wersji development wszystko działa jak trzeba. Studiowałem logi i jakimś cudem w środowisku produkcyjnym zapytanie inkrementujące pole z licznikiem jest wykonywane dwa razy:

Started POST "/comments" for 127.0.0.1 at Mon May 21 14:00:07 +0200 2012 Processing by CommentsController#create as HTML Parameters: {"authenticity_token"=>"/2HRoMq/I+fPzLlKB8CezWEpwjywHo5RwCgm2VRj0zo=", "utf8"=>"✓", "commit"=>"Dodaj", "comment"=>{"content_id"=>"2803", "value"=>"ile doda?", "content_type"=>"Post"}} e[1me[36mUser Load (0.5ms)e[0m e[1mSELECT `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1e[0m e[1me[35m (0.2ms)e[0m BEGIN e[1me[36m (0.6ms)e[0m e[1mUPDATE `users` SET `updated_at` = '2012-05-21 12:00:08', `last_request_at` = '2012-05-21 12:00:08', `perishable_token` = '0uquomSLfT9y1XtIlWay' WHERE `users`.`id` = 1e[0m e[1me[35m (35.4ms)e[0m COMMIT e[1me[36m (0.2ms)e[0m e[1mBEGINe[0m e[1me[35mBlacklist Load (0.8ms)e[0m SELECT `blacklists`.* FROM `blacklists` WHERE (user_id = 1 AND end_date > '2012-05-21 12:00:08') e[1me[36mSQL (0.4ms)e[0m e[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 12:00:08', 0, 0, NULL, '2012-05-21 12:00:08', 1, 'ile doda?')e[0m e[1me[35mPost Load (0.9ms)e[0m SELECT `posts`.* FROM `posts` WHERE `posts`.`id` = 2803 LIMIT 1 e[1me[36mSQL (0.5ms)e[0m e[1mUPDATE `posts` SET `comments_count` = COALESCE(`comments_count`, 0) + 1 WHERE `posts`.`id` = 2803e[0m e[1me[35mSQL (0.4ms)e[0m UPDATE `posts` SET `comments_count` = COALESCE(`comments_count`, 0) + 1 WHERE `posts`.`id` = 2803 e[1me[36m (40.7ms)e[0m e[1mCOMMITe[0m Redirected to http://0.0.0.0:3000/news/zobacz/obt-startuje-w-listopadzie-2 Completed 302 Found in 272ms
Kompletnie nie wiem co może być nie tak. W samych kontrolerach nie ma jakieś większej filozofii. Kombinować coś z plikiem config/environments/production.rb czy przyczyna może kryć się gdzie indziej?
Na wszelki wypadek wrzucę odrazu production.rb:

[code]Mmo::Application.configure do

Settings specified here will take precedence over those in config/application.rb

The production environment is meant for finished, “live” apps.

Code is not reloaded between requests

config.cache_classes = true

Full error reports are disabled and caching is turned on

config.consider_all_requests_local = false
config.action_controller.perform_caching = true

Specifies the header that your server uses for sending files

config.action_dispatch.x_sendfile_header = “X-Sendfile”

For nginx:

config.action_dispatch.x_sendfile_header = ‘X-Accel-Redirect’

If you have no front-end server that supports something like X-Sendfile,

just comment this out and Rails will serve the files

See everything in the log (default is :info)

config.log_level = :debug

Use a different logger for distributed setups

config.logger = SyslogLogger.new

Use a different cache store in production

config.cache_store = :mem_cache_store

Disable Rails’s static asset server

In production, Apache or nginx will already do this

config.serve_static_assets = true

Enable serving of images, stylesheets, and javascripts from an asset server

config.action_controller.asset_host = “http://assets.example.com

Disable delivery errors, bad email addresses will be ignored

config.action_mailer.raise_delivery_errors = false

Enable threaded mode

config.threadsafe!

Enable locale fallbacks for I18n (makes lookups for any locale fall back to

the I18n.default_locale when a translation can not be found)

config.i18n.fallbacks = true

Send deprecation notices to registered listeners

config.active_support.deprecation = :notify

Compress JavaScript and CSS

config.assets.compress = true

Don’t fallback to assets pipeline

config.assets.compile = true

Generate digests for assets URLs

config.assets.digest = true
end[/code]
Problem występuje zarówno na webricku, thin jak i na passangerze (wersja produkcyjna, na dev wszystko OK). Proszę o pomoc w rozwikłaniu tej zagadki.
Ruby: ruby 1.8.7 (2012-04-14 patchlevel 361) [x86_64-linux]
Rails: 3.1.4

Problem rozwiązany. Po przejściu z Rails2 moja relacja w modelu wyglądała następująco:

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

Okazuje się, że w nowych railsach pole na licznik (w tym przypadku :comments_count) jest obsługiwane automatycznie. Powyższy zapis powodował, że Railsy najpierw same zwiększały liczbę komentarzy, a potem “czynił” to jeszcze stary wpis przy relacji.

Trochę szkoda, że nigdzie o tym nie jest wspomniane.