【发布时间】:2012-02-22 21:45:37
【问题描述】:
使用 Rails 3.1.3,我试图弄清楚为什么我们的计数器缓存在通过 update_attributes 更改父记录 ID 时没有正确更新。
class ExhibitorRegistration < ActiveRecord::Base
belongs_to :event, :counter_cache => true
end
class Event < ActiveRecord::Base
has_many :exhibitor_registrations, :dependent => :destroy
end
describe ExhibitorRegistration do
it 'correctly maintains the counter cache on events' do
event = Factory(:event)
other_event = Factory(:event)
registration = Factory(:exhibitor_registration, :event => event)
event.reload
event.exhibitor_registrations_count.should == 1
registration.update_attributes(:event_id => other_event.id)
event.reload
event.exhibitor_registrations_count.should == 0
other_event.reload
other_event.exhibitor_registrations_count.should == 1
end
end
此规范失败,表明事件中的计数器缓存未递减。
1) ExhibitorRegistration correctly maintains the counter cache on events
Failure/Error: event.exhibitor_registrations_count.should == 0
expected: 0
got: 1 (using ==)
我是否应该期望这能正常工作,还是我需要手动跟踪更改并自己更新计数器?
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-3.1 rails-activerecord