Rails 3.2.14
Ruby 2.1.7
When using counter_cache, the count column gets incremented by 2 instead of 1. For example:
class Finger < BaseModel
belongs_to :hand, :counter_cache => true
end
class Hand < BaseModel
has_many :fingers
end
So if I do:
finger = Finger.new(:hand_id => 1)
finger.save
I can see in my console:
(0.2ms) BEGIN
SQL (4.3ms) INSERT INTO "fingers" ("hand_id") VALUES ($1) RETURNING "id" [["hand_id", 311222]
Hand Load (0.4ms) SELECT "hands".* FROM "hands" WHERE "hands"."id" = 311222 LIMIT 1
SQL (0.7ms) UPDATE "hands" SET "fingers_count" = COALESCE("fingers_count", 0) + 1 WHERE "hands"."id" = 311222
(8.2ms) COMMIT
=> true
So as you can see, COALESCE only gets called once.
But then:
Hand[1].fingers_count
> 2
But:
Hand[1].fingers.count
> 1
In my application, this is all done with out-of-the-box rails forms that create the new Finger related to the Hand.