counter_cache没有递增......我做错了什么?

时间:2012-02-04 04:42:22

标签: ruby-on-rails activerecord counter-cache

这看起来非常简单,让我的生活变得悲惨。 Rails 3.1.3。

修改:根据以下建议添加了t.integer" versions_count",:default => 0到迁移,回滚,重新创建数据库。现在versions_count保持为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

控制台:

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">

请帮忙,让我疯了。

编辑2:在删除相关记录时,计数器似乎会减去其值。但它永远不会得到补充。

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 个答案:

答案 0 :(得分:0)

我不认为counter_cache的工作原理是nil。您需要设置默认值0

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