ActiveRecord has_many:通过复制质量分配的计数器缓存

Car*_*auf 6 ruby activerecord ruby-on-rails has-many-through

似乎ActiveRecord的counter_cache功能可能导致计数器缓存增加两次.我看到这种行为的场景是当我有两个模型has_many :through通过连接模型彼此建立关系时(即:Teacher有很多Student通过Classroom).使用has_many :through生成的方法直接关联教师和学生(无需手动创建连接记录)时,计数会增加2倍.示例:teacher.students << Student.create(name: "Bobby Joe")原因teacher.students_count增加2.

请帮助我找到一个解决方案,减轻或消除这个问题,同时允许我继续使用内置的计数器缓存和通过has_many :through关系的质量分配.

我花了很多时间寻找解决方案并将问题提取到一个小的测试应用程序,这是我可以创建的最简单的失败示例.任何有助于我解决这个问题的额外细节都应该在下面.

示例模式和模型:

create_table :teachers do |t|
  t.string  :name
  t.integer :students_count, default: 0
  t.timestamps
end

class Teacher < ActiveRecord::Base
  has_many :classrooms
  has_many :students, :through => :classrooms
end

create_table :students do |t|
  t.string  :name
  t.integer :teachers_count, default: 0
  t.timestamps
end

class Student < ActiveRecord::Base
  has_many :classrooms
  has_many :teachers, :through => :classrooms
end

create_table :classrooms do |t|
  t.references :teacher
  t.references :student
  t.timestamps
end

class Classroom < ActiveRecord::Base
  belongs_to :student, :counter_cache => :teachers_count
  belongs_to :teacher, :counter_cache => :students_count
end
Run Code Online (Sandbox Code Playgroud)

这是一个简短的rails控制台会话,显示了所采取的步骤以及rails正在执行两个更新teachers以增加的事实students_count:

1.9.2-p290 :001 > t = Teacher.create(name: "Miss Nice")
  SQL (9.7ms)  INSERT INTO "teachers" ("created_at", "name", "students_count", "updated_at") VALUES (?, ?, ?, ?)  [["created_at", Tue, 28 Feb 2012 03:31:53 UTC +00:00], ["name", "Miss Nice"], ["students_count", 0], ["updated_at", Tue, 28 Feb 2012 03:31:53 UTC +00:00]]
 => #<Teacher id: 1, name: "Miss Nice", students_count: 0, created_at: "2012-02-28 03:31:53", updated_at: "2012-02-28 03:31:53"> 
1.9.2-p290 :002 > t.students << Student.new(name: "Mary Ann")
  SQL (0.3ms)  INSERT INTO "students" ("created_at", "name", "teachers_count", "updated_at") VALUES (?, ?, ?, ?)  [["created_at", Tue, 28 Feb 2012 03:32:12 UTC +00:00], ["name", "Mary Ann"], ["teachers_count", 0], ["updated_at", Tue, 28 Feb 2012 03:32:12 UTC +00:00]]
  SQL (0.3ms)  INSERT INTO "classrooms" ("created_at", "student_id", "teacher_id", "updated_at") VALUES (?, ?, ?, ?)  [["created_at", Tue, 28 Feb 2012 03:32:12 UTC +00:00], ["student_id", 1], ["teacher_id", 1], ["updated_at", Tue, 28 Feb 2012 03:32:12 UTC +00:00]]
  SQL (0.2ms)  UPDATE "students" SET "teachers_count" = COALESCE("teachers_count", 0) + 1 WHERE "students"."id" = 1
  Teacher Load (0.1ms)  SELECT "teachers".* FROM "teachers" WHERE "teachers"."id" = 1 LIMIT 1
  SQL (0.1ms)  UPDATE "teachers" SET "students_count" = COALESCE("students_count", 0) + 1 WHERE "teachers"."id" = 1
  SQL (0.0ms)  UPDATE "teachers" SET "students_count" = COALESCE("students_count", 0) + 1 WHERE "teachers"."id" = 1
  Student Load (0.2ms)  SELECT "students".* FROM "students" INNER JOIN "classrooms" ON "students"."id" = "classrooms"."student_id" WHERE "classrooms"."teacher_id" = 1
 => [#<Student id: 1, name: "Mary Ann", teachers_count: 1, created_at: "2012-02-28 03:32:12", updated_at: "2012-02-28 03:32:12">] 
Run Code Online (Sandbox Code Playgroud)

我把整个测试应用程序放在github上,如果有人想看得更近(https://github.com/carlzulauf/test_app).我还创建了一个单元测试来演示这个问题并且无法通过(https://github.com/carlzulauf/test_app/blob/master/test/unit/classroom_test.rb)

Car*_*auf 12

到目前为止,我的研究告诉我这可能是一个错误.以下是已针对此问题提交的一些github问题:

https://github.com/rails/rails/issues/3903

https://github.com/rails/rails/issues/3085

显然有一个由has_many:通过关系引起的无证自动计数器缓存.因此,如果Teacher.has_many :students, :through => :classrooms那时teacher.students << student收集分配已经查找并且teacher.students_count如果该列存在则递增.

如果添加,Classroom.belongs_to :teacher, :counter_cache => :students_count则在创建"课堂"模型时会触发其他回调,并且该列会增加两次.

有效解决方法:将计数器缓存列重命名为其他内容.Student#teacherz_count并且Teacher#studentz_count有效地允许我的测试用例通过.

https://github.com/carlzulauf/test_app/commit/707a33f948d5d55a8aa942e825841fdd8a7e7705

我还没有找到问题在ActiveRecord代码库中的位置,所以我暂时不会接受我自己的答案,以防有人知道为什么这样has_many :through工作以及有问题的代码存在的地方.

更新

我相信我发现了令人讨厌的代码行.注释掉这一行解决了这个问题:

https://github.com/rails/rails/blob/889e8bee82ea4f75adb6de5badad512d2c615b7f/activerecord/lib/active_record/associations/has_many_through_association.rb#L53

我似乎无法获得正常运行,因此我无法提交此错误.如果其他人能够,请做.

找到违规行允许我在我的测试应用程序中制作一个更有效的猴子补丁,解决问题而不重命名任何列.

https://github.com/carlzulauf/test_app/commit/3c421b035bd032b91ff60e3d74b957651c37c7fa