0

这看起来超级简单,让我的生活变得悲惨。导轨 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 > 
4

1 回答 1

0

我不认为 counter_cache 从零开始工作。您需要将默认值设为 0

add_column :documents, : versions_count, :integer, :default => 0
于 2012-02-04T05:17:57.287 回答