Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
f1dcfc6c
R
rails
项目概览
张重言
/
rails
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
rails
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
f1dcfc6c
编写于
9月 20, 2018
作者:
R
Ryuta Kamizono
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #33913 from kamipo/counter_cache
Don't update counter cache unless the record is actually saved
上级
4586b304
fe929c17
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
43 addition
and
29 deletion
+43
-29
activerecord/CHANGELOG.md
activerecord/CHANGELOG.md
+6
-0
activerecord/lib/active_record/associations/belongs_to_association.rb
.../lib/active_record/associations/belongs_to_association.rb
+0
-16
activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb
...record/associations/belongs_to_polymorphic_association.rb
+0
-4
activerecord/lib/active_record/associations/builder/belongs_to.rb
...cord/lib/active_record/associations/builder/belongs_to.rb
+1
-3
activerecord/test/cases/associations/belongs_to_associations_test.rb
...d/test/cases/associations/belongs_to_associations_test.rb
+36
-6
未找到文件。
activerecord/CHANGELOG.md
浏览文件 @
f1dcfc6c
*
Don't update counter cache unless the record is actually saved.
Fixes #31493, #33113, #33117.
*Ryuta Kamizono*
*
Deprecate
`ActiveRecord::Result#to_hash`
in favor of
`ActiveRecord::Result#to_a`
.
*Gannon McGibbon*, *Kevin Cheng*
...
...
activerecord/lib/active_record/associations/belongs_to_association.rb
浏览文件 @
f1dcfc6c
...
...
@@ -50,11 +50,8 @@ def target_changed?
def
replace
(
record
)
if
record
raise_on_type_mismatch!
(
record
)
update_counters_on_replace
(
record
)
set_inverse_instance
(
record
)
@updated
=
true
else
decrement_counters
end
replace_keys
(
record
)
...
...
@@ -80,19 +77,6 @@ def require_counter_update?
reflection
.
counter_cache_column
&&
owner
.
persisted?
end
def
update_counters_on_replace
(
record
)
if
require_counter_update?
&&
different_target?
(
record
)
owner
.
instance_variable_set
:@_after_replace_counter_called
,
true
record
.
increment!
(
reflection
.
counter_cache_column
,
touch:
reflection
.
options
[
:touch
])
decrement_counters
end
end
# Checks whether record is different to the current target, without loading it
def
different_target?
(
record
)
record
.
_read_attribute
(
primary_key
(
record
))
!=
owner
.
_read_attribute
(
reflection
.
foreign_key
)
end
def
replace_keys
(
record
)
owner
[
reflection
.
foreign_key
]
=
record
?
record
.
_read_attribute
(
primary_key
(
record
))
:
nil
end
...
...
activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb
浏览文件 @
f1dcfc6c
...
...
@@ -19,10 +19,6 @@ def replace_keys(record)
owner
[
reflection
.
foreign_type
]
=
record
?
record
.
class
.
polymorphic_name
:
nil
end
def
different_target?
(
record
)
super
||
record
.
class
!=
klass
end
def
inverse_reflection_for
(
record
)
reflection
.
polymorphic_inverse_of
(
record
.
class
)
end
...
...
activerecord/lib/active_record/associations/builder/belongs_to.rb
浏览文件 @
f1dcfc6c
...
...
@@ -34,9 +34,7 @@ def belongs_to_counter_cache_after_update(reflection)
foreign_key
=
reflection
.
foreign_key
cache_column
=
reflection
.
counter_cache_column
if
@_after_replace_counter_called
||=
false
@_after_replace_counter_called
=
false
elsif
association
(
reflection
.
name
).
target_changed?
if
association
(
reflection
.
name
).
target_changed?
if
reflection
.
polymorphic?
model
=
attribute_in_database
(
reflection
.
foreign_type
).
try
(
:constantize
)
model_was
=
attribute_before_last_save
(
reflection
.
foreign_type
).
try
(
:constantize
)
...
...
activerecord/test/cases/associations/belongs_to_associations_test.rb
浏览文件 @
f1dcfc6c
...
...
@@ -452,15 +452,39 @@ def test_belongs_to_counter
end
def
test_belongs_to_counter_with_assigning_nil
post
=
Post
.
find
(
1
)
comment
=
Comment
.
find
(
1
)
topic
=
Topic
.
create!
(
title:
"debate"
)
reply
=
Reply
.
create!
(
title:
"blah!"
,
content:
"world around!"
,
topic:
topic
)
assert_equal
post
.
id
,
comment
.
pos
t_id
assert_equal
2
,
Post
.
find
(
post
.
id
).
comment
s
.
size
assert_equal
topic
.
id
,
reply
.
paren
t_id
assert_equal
1
,
topic
.
reload
.
replie
s
.
size
comment
.
post
=
nil
reply
.
topic
=
nil
reply
.
reload
assert_equal
1
,
Post
.
find
(
post
.
id
).
comments
.
size
assert_equal
topic
.
id
,
reply
.
parent_id
assert_equal
1
,
topic
.
reload
.
replies
.
size
reply
.
topic
=
nil
reply
.
save!
assert_equal
0
,
topic
.
reload
.
replies
.
size
end
def
test_belongs_to_counter_with_assigning_new_object
topic
=
Topic
.
create!
(
title:
"debate"
)
reply
=
Reply
.
create!
(
title:
"blah!"
,
content:
"world around!"
,
topic:
topic
)
assert_equal
topic
.
id
,
reply
.
parent_id
assert_equal
1
,
topic
.
reload
.
replies_count
topic2
=
reply
.
build_topic
(
title:
"debate2"
)
reply
.
save!
assert_not_equal
topic
.
id
,
reply
.
parent_id
assert_equal
topic2
.
id
,
reply
.
parent_id
assert_equal
0
,
topic
.
reload
.
replies_count
assert_equal
1
,
topic2
.
reload
.
replies_count
end
def
test_belongs_to_with_primary_key_counter
...
...
@@ -485,11 +509,13 @@ def test_belongs_to_with_primary_key_counter
assert_equal
0
,
debate2
.
reload
.
replies_count
reply
.
topic_with_primary_key
=
debate2
reply
.
save!
assert_equal
0
,
debate
.
reload
.
replies_count
assert_equal
1
,
debate2
.
reload
.
replies_count
reply
.
topic_with_primary_key
=
nil
reply
.
save!
assert_equal
0
,
debate
.
reload
.
replies_count
assert_equal
0
,
debate2
.
reload
.
replies_count
...
...
@@ -516,11 +542,13 @@ def test_belongs_to_counter_with_reassigning
assert_equal
1
,
Topic
.
find
(
topic2
.
id
).
replies
.
size
reply1
.
topic
=
nil
reply1
.
save!
assert_equal
0
,
Topic
.
find
(
topic1
.
id
).
replies
.
size
assert_equal
0
,
Topic
.
find
(
topic2
.
id
).
replies
.
size
reply1
.
topic
=
topic1
reply1
.
save!
assert_equal
1
,
Topic
.
find
(
topic1
.
id
).
replies
.
size
assert_equal
0
,
Topic
.
find
(
topic2
.
id
).
replies
.
size
...
...
@@ -594,6 +622,7 @@ def test_belongs_to_touch_with_reassigning
debate2
.
touch
(
time:
time
)
reply
.
topic_with_primary_key
=
debate2
reply
.
save!
assert_operator
debate
.
reload
.
updated_at
,
:
>
,
time
assert_operator
debate2
.
reload
.
updated_at
,
:
>
,
time
...
...
@@ -772,6 +801,7 @@ def test_counter_cache
reply
=
Reply
.
create
(
title:
"re: zoom"
,
content:
"speedy quick!"
)
reply
.
topic
=
topic
reply
.
save!
assert_equal
1
,
topic
.
reload
[
:replies_count
]
assert_equal
1
,
topic
.
replies
.
size
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录