counter_cache not incrementing... what am I doing wrong?

335 views Asked by At

This seems super simple and so is making my life miserable. Rails 3.1.3.

Edit: Based on advise below added t.integer "versions_count", :default => 0 to the migration,rolled back, recreated the database. Now the versions_count stays at 0.

#version.rb:      
class Version < ActiveRecord::Base
  belongs_to :document, :counter_cache => true
end

#document.rb
class Document < ActiveRecord::Base
  has_many :versions
end

#schema.rb:
create_table "documents", :force => true do |t|
  t.text     "name"
  #EDIT: Added a default (see below)  t.integer  "versions_count"
  t.integer  "versions_count", :default => 0
  t.datetime "created_at"
  t.datetime "updated_at"
end

create_table "versions", :force => true do |t|
  t.text     "name"
  t.integer  "document_id"
  t.datetime "created_at"
  t.datetime "updated_at"
end

Console:

1.9.2-p290 :001 > d = Document.new
 => #<Document id: nil, name: nil, versions_count: nil, created_at: nil, updated_at: nil> 
1.9.2-p290 :002 > d.name = "doc 1"
 => "doc 1" 
1.9.2-p290 :003 > d.save
  SQL (79.9ms)  INSERT INTO "documents" ("created_at", "name", "updated_at", "versions_count") VALUES (?, ?, ?, ?)  [["created_at", Sat, 04 Feb 2012 04:34:51 UTC +00:00], ["name", "doc 1"], ["updated_at", Sat, 04 Feb 2012 04:34:51 UTC +00:00], ["versions_count", nil]]
 => true 
1.9.2-p290 :004 > v = Version.new
 => #<Version id: nil, name: nil, document_id: nil, created_at: nil, updated_at: nil> 
1.9.2-p290 :005 > v.name = "v1"
 => "v1" 
1.9.2-p290 :006 > v.save
  SQL (1.0ms)  INSERT INTO "versions" ("created_at", "document_id", "name", "updated_at") VALUES (?, ?, ?, ?)  [["created_at", Sat, 04 Feb 2012 04:35:13 UTC +00:00], ["document_id", nil], ["name", "v1"], ["updated_at", Sat, 04 Feb 2012 04:35:13 UTC +00:00]]
 => true 
1.9.2-p290 :007 > d
 => #<Document id: 1, name: "doc 1", versions_count: nil, created_at: "2012-02-04 04:34:51", updated_at: "2012-02-04 04:34:51"> 
1.9.2-p290 :008 > d.versions << v
   (0.3ms)  UPDATE "versions" SET "document_id" = 1, "updated_at" = '2012-02-04 04:35:24.490334' WHERE "versions"."id" = 1
  Version Load (0.2ms)  SELECT "versions".* FROM "versions" WHERE "versions"."document_id" = 1
 => [#<Version id: 1, name: "v1", document_id: 1, created_at: "2012-02-04 04:35:13", updated_at: "2012-02-04 04:35:24">] 
1.9.2-p290 :009 > d.save
 => true 
1.9.2-p290 :010 > d.versions_count
 => nil 
1.9.2-p290 :016 >   v.save
 => true 
1.9.2-p290 :017 > d
 => #<Document id: 1, name: "doc 1", versions_count: nil, created_at: "2012-02-04 04:34:51", updated_at: "2012-02-04 04:34:51"> 
1.9.2-p290 :018 > d2 = Document.first
  Document Load (0.3ms)  SELECT "documents".* FROM "documents" LIMIT 1
 => #<Document id: 1, name: "doc 1", versions_count: nil, created_at: "2012-02-04 04:34:51", updated_at: "2012-02-04 04:34:51">

Please help, driving me nuts.

Edit 2: It seems that the counter gets its value subtracted when removing the associated records. But it never gets the additions.

1.9.2-p290 :200 >   d = Document.first
  Document Load (0.4ms)  SELECT "documents".* FROM "documents" LIMIT 1
 => #<Document id: 1, name: "doc 1", versions_count: 0, created_at: "2012-02-04 21:49:31", updated_at: "2012-02-04 22:15:25"> 
1.9.2-p290 :201 > d.versions
  Version Load (0.3ms)  SELECT "versions".* FROM "versions" WHERE "versions"."document_id" = 1
 => [] 
1.9.2-p290 :202 > v1 = Version.first
  Version Load (0.3ms)  SELECT "versions".* FROM "versions" LIMIT 1
 => #<Version id: 1, name: "v1", document_id: nil, created_at: "2012-02-04 21:49:31", updated_at: "2012-02-04 22:13:47"> 
1.9.2-p290 :203 > v2 = Version.last
  Version Load (0.3ms)  SELECT "versions".* FROM "versions" ORDER BY "versions"."id" DESC LIMIT 1
 => #<Version id: 2, name: "v2", document_id: nil, created_at: "2012-02-04 21:59:12", updated_at: "2012-02-04 22:11:37"> 
1.9.2-p290 :204 > d.versions << v1
   (1.5ms)  UPDATE "versions" SET "document_id" = 1, "updated_at" = '2012-02-04 22:16:09.876354' WHERE "versions"."id" = 1
 => [#<Version id: 1, name: "v1", document_id: 1, created_at: "2012-02-04 21:49:31", updated_at: "2012-02-04 22:16:09">] 
1.9.2-p290 :205 > d.versions << v2
   (0.6ms)  UPDATE "versions" SET "document_id" = 1, "updated_at" = '2012-02-04 22:16:15.628396' WHERE "versions"."id" = 2
 => [#<Version id: 1, name: "v1", document_id: 1, created_at: "2012-02-04 21:49:31", updated_at: "2012-02-04 22:16:09">, #<Version id: 2, name: "v2", document_id: 1, created_at: "2012-02-04 21:59:12", updated_at: "2012-02-04 22:16:15">] 
1.9.2-p290 :206 > d.save
 => true 
1.9.2-p290 :207 > d
 => #<Document id: 1, name: "doc 1", versions_count: 0, created_at: "2012-02-04 21:49:31", updated_at: "2012-02-04 22:15:25"> 
1.9.2-p290 :208 > d.versions
 => [#<Version id: 1, name: "v1", document_id: 1, created_at: "2012-02-04 21:49:31", updated_at: "2012-02-04 22:16:09">, #<Version id: 2, name: "v2", document_id: 1, created_at: "2012-02-04 21:59:12", updated_at: "2012-02-04 22:16:15">] 
1.9.2-p290 :209 > d.versions = []
  SQL (1.8ms)  UPDATE "versions" SET "document_id" = NULL WHERE "versions"."document_id" = 1
  SQL (0.2ms)  UPDATE "documents" SET "versions_count" = COALESCE("versions_count", 0) - 2 WHERE "documents"."id" = 1
 => [] 
1.9.2-p290 :210 > d
 => #<Document id: 1, name: "doc 1", versions_count: -2, created_at: "2012-02-04 21:49:31", updated_at: "2012-02-04 22:15:25"> 
1.9.2-p290 :211 > 
1

There are 1 answers

2
Mike Vormwald On

I don't think the counter_cache works from nil. You need to make the default 0

add_column :documents, : versions_count, :integer, :default => 0