Rails ActiveRecord:如果在创建之前未设置关联,则 counter_cache 不会更新。故意?

发布于 2024-11-25 18:10:15 字数 1940 浏览 1 评论 0原文

我已经实现了与 :counter_cache => 的 own_to 关系true 并且我注意到,如果在初始保存之前未设置关系,则计数器缓存不会更新。

例如,假设一家公司有很多员工。如果我这样做,

company.employees << Employee.new(:name => "Joe")

计数器会正确更新,但如果我这样做,

company.employees << Employee.create(:name => "Joe")

计数器保持不变。

有关更多详细信息,这里是模型:

class Employee < ActiveRecord::Base
  belongs_to :company, :counter_cache => true
end

class Company < ActiveRecord::Base
  has_many :employees
end

这里有一个 Rails 控制台会话演示了这一点:

Loading development environment (Rails 3.0.5)
ruby-1.9.2-p180 :001 > company_a = Company.create(:name => "ACME")
 => #<Company id: 1, name: "ACME", created_at: "2011-07-22 01:31:39", updated_at: "2011-07-22 01:31:39", employees_count: 0> 
ruby-1.9.2-p180 :002 > company_a.employees << Employee.new(:name => "Bob")
 => [#<Employee id: 1, company_id: 1, name: "Bob", created_at: "2011-07-22 01:31:59", updated_at: "2011-07-22 01:31:59">] 
ruby-1.9.2-p180 :003 > company_a.reload
 => #<Company id: 1, name: "ACME", created_at: "2011-07-22 01:31:39", updated_at: "2011-07-22 01:31:39", employees_count: 1> 
ruby-1.9.2-p180 :004 > company_a.employees << Employee.create(:name => "Joe")
 => [#<Employee id: 1, company_id: 1, name: "Bob", created_at: "2011-07-22 01:31:59", updated_at: "2011-07-22 01:31:59">, #<Employee id: 2, company_id: 1, name: "Joe", created_at: "2011-07-22 01:32:28", updated_at: "2011-07-22 01:32:28">] 
ruby-1.9.2-p180 :005 > company_a.reload
 => #<Company id: 1, name: "ACME", created_at: "2011-07-22 01:31:39", updated_at: "2011-07-22 01:31:39", employees_count: 1> 

文档确实说计数器在创建/销毁对象时递增/递减,但我认为它也应该监视更新以使其有用。否则,比如说,在公司之间调动员工很快就会导致柜台完全关闭。

这是预期的行为吗?如果是这样,其理由是什么?如果不是,我做错了什么吗?我在 Rails 3.0.5 和 Ruby 1.9.2 中尝试过,

谢谢!

I've implemented a belongs_to relation with :counter_cache => true and I notice that the counter cache does not get updated if the relation was not set up before the initial save.

For instance, say a company has_many employees. If I do

company.employees << Employee.new(:name => "Joe")

The counter gets updated correctly but if I do

company.employees << Employee.create(:name => "Joe")

The counter remains unchanged.

For more details, here are the models:

class Employee < ActiveRecord::Base
  belongs_to :company, :counter_cache => true
end

class Company < ActiveRecord::Base
  has_many :employees
end

And here's a Rails Console session that demonstrates this:

Loading development environment (Rails 3.0.5)
ruby-1.9.2-p180 :001 > company_a = Company.create(:name => "ACME")
 => #<Company id: 1, name: "ACME", created_at: "2011-07-22 01:31:39", updated_at: "2011-07-22 01:31:39", employees_count: 0> 
ruby-1.9.2-p180 :002 > company_a.employees << Employee.new(:name => "Bob")
 => [#<Employee id: 1, company_id: 1, name: "Bob", created_at: "2011-07-22 01:31:59", updated_at: "2011-07-22 01:31:59">] 
ruby-1.9.2-p180 :003 > company_a.reload
 => #<Company id: 1, name: "ACME", created_at: "2011-07-22 01:31:39", updated_at: "2011-07-22 01:31:39", employees_count: 1> 
ruby-1.9.2-p180 :004 > company_a.employees << Employee.create(:name => "Joe")
 => [#<Employee id: 1, company_id: 1, name: "Bob", created_at: "2011-07-22 01:31:59", updated_at: "2011-07-22 01:31:59">, #<Employee id: 2, company_id: 1, name: "Joe", created_at: "2011-07-22 01:32:28", updated_at: "2011-07-22 01:32:28">] 
ruby-1.9.2-p180 :005 > company_a.reload
 => #<Company id: 1, name: "ACME", created_at: "2011-07-22 01:31:39", updated_at: "2011-07-22 01:31:39", employees_count: 1> 

The documentation does say that the counter is incremented/decremented when the object is created/destroyed but I was thinking it should monitor updates as well to be useful. Otherwise, say, moving employees between companies would quickly result in counters that are totally off.

Is this the expected behavior? If so, what's the rationale? And if not, am I doing something wrong? I tried this in Rails 3.0.5 and Ruby 1.9.2

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文