提交 23bb8d77 编写于 作者: S Sean Griffin

Correct errors in counter cache updating

The cache name should be converted to a string when given, not compared
as a symbol. This edge case is already adequately covered by our tests,
but was masked by another issue where we were incorrectly updating the
counter cache twice. When paired with a bug where we didn't update the
counter cache because we couldn't find a match with the name, this made
it look like everything was working fine.

Fixes #10865.
上级 4a62724c
* Fixed several edge cases which could result in a counter cache updating
twice or not updating at all for `has_many` and `has_many :through`.
Fixes #10865.
*Sean Griffin*
* Foreign keys added by migrations were given random, generated names. This
meant a different `structure.sql` would be generated every time a developer
ran migrations on their machine.
......
......@@ -85,7 +85,11 @@ def has_cached_counter?(reflection = reflection())
end
def cached_counter_attribute_name(reflection = reflection())
options[:counter_cache] || "#{reflection.name}_count"
if reflection.options[:counter_cache]
reflection.options[:counter_cache].to_s
else
"#{reflection.name}_count"
end
end
def update_counter(difference, reflection = reflection())
......
......@@ -160,9 +160,9 @@ def delete_records(records, method)
if through_reflection.collection? && update_through_counter?(method)
update_counter(-count, through_reflection)
else
update_counter(-count)
end
update_counter(-count)
end
def through_records_for(record)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册