提交 2ea922bc 编写于 作者: T Tarmo Tänav 提交者: Santiago Pastorino

Don't increment and then decrement the same counter when re-assigning a belongs_to association

[#2786 state:committed]
Signed-off-by: NSantiago Pastorino <santiago@wyeworks.com>
上级 ccd45618
......@@ -22,7 +22,7 @@ def replace(record)
else
raise_on_type_mismatch(record)
if counter_cache_name && !@owner.new_record?
if counter_cache_name && !@owner.new_record? && record.id != @owner[@reflection.primary_key_name]
@reflection.klass.increment_counter(counter_cache_name, record.id)
@reflection.klass.decrement_counter(counter_cache_name, @owner[@reflection.primary_key_name]) if @owner[@reflection.primary_key_name]
end
......
......@@ -215,6 +215,10 @@ def test_belongs_to_counter_with_reassigning
r1.topic = Topic.find(t2.id)
assert_no_queries do
r1.topic = t2
end
assert r1.save
assert_equal 0, Topic.find(t1.id).replies.size
assert_equal 1, Topic.find(t2.id).replies.size
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册