감안할 때 나는이 다음 모델 : 나는 다음과 같은 사양을 실행하면<< 연결 일 때 counter_cache 열이 증가하지 않는 이유는 무엇입니까?
class Location < Active::Record
has_many :storables, foreign_key: :bin_id
# ...
end
class Storable < Active::Record
belongs_to :bin, class_name: :Location, counter_cache: true
# ...
end
의 counter_cache
이 제대로 증가하지 않습니다. #1
및 #2
은 예상대로 작동하지만 #3
은 작동하지 않습니다. 뭐라 구요?
describe "location storables" do
specify "adding a storable increments the counter cache" do
l = Location.create
l.storables_count.should == 0 #=> PASSES
# method 1
s = Storable.create(bin: l)
l.reload
l.storables_count.should == 1 #=> PASSES
# method 2
l.storables.create
l.reload
l.storables_count.should == 2 #=> PASSES
# method 3
l.storables << Storable.create
l.reload
l.storables_count.should == 3 #=> FAILS, got 2 not 3
end
end
정말 counter_cache 절반 작업에 의해 혼동하고 있습니다. 나는 구성 문제를 발견 할 수 없다.
이 프로젝트에서는 레일 3.2.12을 사용합니다. 4 도움이되지 않았다
UPDATE
업그레이드 에 레일. 또한 방법 3을 다음과 같이 변경하면 테스트가 통과됩니다.
# method 3
l.storables << Storable.create
puts "proxy : #{l.storables.count}" #=> 3
puts "relation : #{Storable.count}" #=> 3
puts "cache : #{l.storables_count}" #=> 2
Location.reset_counters(l.id, :storables) # corrects cache
l.reload
l.storables_count.should == 3 #=> PASSES
왜 자동으로 발생하지 않습니까?
허용되는 대답에 대해서는''l.storables << Storable.last''와 같은 동작을 보게됩니다. 아마도 더 나빠질 것입니다. – dgilperez