提交 9ce59f66 编写于 作者: R Rafael Mendonça França

Merge pull request #15747 from sgrif/sg-trolololol-this-is-so-broken

Always update counter caches in memory when adding records
......@@ -83,6 +83,13 @@ def update_counter(difference, reflection = reflection())
if has_cached_counter?(reflection)
counter = cached_counter_attribute_name(reflection)
owner.class.update_counters(owner.id, counter => difference)
update_counter_in_memory(difference, reflection)
end
end
def update_counter_in_memory(difference, reflection = reflection())
if has_cached_counter?(reflection)
counter = cached_counter_attribute_name(reflection)
owner[counter] += difference
owner.changed_attributes.delete(counter) # eww
end
......@@ -137,6 +144,25 @@ def foreign_key_present?
false
end
end
def concat_records(records, *)
update_counter_if_success(super, records.length)
end
def _create_record(attributes, *)
if attributes.is_a?(Array)
super
else
update_counter_if_success(super, 1)
end
end
def update_counter_if_success(saved_successfully, difference)
if saved_successfully
update_counter_in_memory(difference)
end
saved_successfully
end
end
end
end
......@@ -63,7 +63,6 @@ def insert_record(record, validate = true, raise = false)
end
save_through_record(record)
update_counter(1)
record
end
......
......@@ -772,6 +772,36 @@ def test_deleting_updates_counter_cache
assert_equal topic.replies.to_a.size, topic.replies_count
end
def test_counter_cache_updates_in_memory_after_concat
topic = Topic.create title: "Zoom-zoom-zoom"
topic.replies << Reply.create(title: "re: zoom", content: "speedy quick!")
assert_equal 1, topic.replies_count
assert_equal 1, topic.replies.size
assert_equal 1, topic.reload.replies.size
end
def test_counter_cache_updates_in_memory_after_create
topic = Topic.create title: "Zoom-zoom-zoom"
topic.replies.create!(title: "re: zoom", content: "speedy quick!")
assert_equal 1, topic.replies_count
assert_equal 1, topic.replies.size
assert_equal 1, topic.reload.replies.size
end
def test_counter_cache_updates_in_memory_after_create_with_array
topic = Topic.create title: "Zoom-zoom-zoom"
topic.replies.create!([
{ title: "re: zoom", content: "speedy quick!" },
{ title: "re: zoom 2", content: "OMG lol!" },
])
assert_equal 2, topic.replies_count
assert_equal 2, topic.replies.size
assert_equal 2, topic.reload.replies.size
end
def test_pushing_association_updates_counter_cache
topic = Topic.order("id ASC").first
reply = Reply.create!
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册