Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
66679c8e
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,体验更适合开发者的 AI 搜索 >>
提交
66679c8e
编写于
9月 17, 2012
作者:
I
Ian Young
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Update other counter caches on destroy
上级
34c7e73c
变更
11
隐藏空白更改
内联
并排
Showing
11 changed file
with
45 addition
and
9 deletion
+45
-9
activerecord/CHANGELOG.md
activerecord/CHANGELOG.md
+5
-0
activerecord/lib/active_record/associations/builder/belongs_to.rb
...cord/lib/active_record/associations/builder/belongs_to.rb
+1
-1
activerecord/lib/active_record/associations/has_many_association.rb
...rd/lib/active_record/associations/has_many_association.rb
+1
-1
activerecord/lib/active_record/autosave_association.rb
activerecord/lib/active_record/autosave_association.rb
+14
-0
activerecord/lib/active_record/core.rb
activerecord/lib/active_record/core.rb
+1
-0
activerecord/test/cases/counter_cache_test.rb
activerecord/test/cases/counter_cache_test.rb
+8
-0
activerecord/test/fixtures/dog_lovers.yml
activerecord/test/fixtures/dog_lovers.yml
+3
-0
activerecord/test/fixtures/dogs.yml
activerecord/test/fixtures/dogs.yml
+1
-0
activerecord/test/models/dog.rb
activerecord/test/models/dog.rb
+3
-2
activerecord/test/models/dog_lover.rb
activerecord/test/models/dog_lover.rb
+3
-2
activerecord/test/schema/schema.rb
activerecord/test/schema/schema.rb
+5
-3
未找到文件。
activerecord/CHANGELOG.md
浏览文件 @
66679c8e
## Rails 4.0.0 (unreleased) ##
*
Models with multiple counter cache associations now update correctly on destroy.
See #7706.
*Ian Young*
*
If inverse_of is true on an association, then when one calls +find()+ on
the association, ActiveRecord will first look through the in-memory objects
in the association for a particular id. Then, it will go to the DB if it
...
...
activerecord/lib/active_record/associations/builder/belongs_to.rb
浏览文件 @
66679c8e
...
...
@@ -31,7 +31,7 @@ def belongs_to_counter_cache_after_create_for_#{name}
end
def belongs_to_counter_cache_before_destroy_for_
#{
name
}
unless
marked_for_destruction?
unless
destroyed_by_association && destroyed_by_association.foreign_key.to_sym ==
#{
foreign_key
.
to_sym
.
inspect
}
record =
#{
name
}
record.class.decrement_counter(:
#{
cache_column
}
, record.id) unless record.nil?
end
...
...
activerecord/lib/active_record/associations/has_many_association.rb
浏览文件 @
66679c8e
...
...
@@ -22,7 +22,7 @@ def handle_dependency
else
if
options
[
:dependent
]
==
:destroy
# No point in executing the counter update since we're going to destroy the parent anyway
load_target
.
each
(
&
:mark_for_destruction
)
load_target
.
each
{
|
t
|
t
.
destroyed_by_association
=
reflection
}
destroy_all
else
delete_all
...
...
activerecord/lib/active_record/autosave_association.rb
浏览文件 @
66679c8e
...
...
@@ -212,6 +212,7 @@ def add_autosave_association_callbacks(reflection)
# Reloads the attributes of the object as usual and clears <tt>marked_for_destruction</tt> flag.
def
reload
(
options
=
nil
)
@marked_for_destruction
=
false
@destroyed_by_association
=
nil
super
end
...
...
@@ -231,6 +232,19 @@ def marked_for_destruction?
@marked_for_destruction
end
# Records the association that is being destroyed and destroying this
# record in the process.
def
destroyed_by_association
=
(
reflection
)
@destroyed_by_association
=
reflection
end
# Returns the association for the parent being destroyed.
#
# Used to avoid updating the counter cache unnecessarily.
def
destroyed_by_association
@destroyed_by_association
end
# Returns whether or not this record has been changed in any way (including whether
# any of its nested autosave associations are likewise changed)
def
changed_for_autosave?
...
...
activerecord/lib/active_record/core.rb
浏览文件 @
66679c8e
...
...
@@ -427,6 +427,7 @@ def init_internals
@readonly
=
false
@destroyed
=
false
@marked_for_destruction
=
false
@destroyed_by_association
=
nil
@new_record
=
true
@txn
=
nil
@_start_transaction_state
=
{}
...
...
activerecord/test/cases/counter_cache_test.rb
浏览文件 @
66679c8e
...
...
@@ -115,6 +115,14 @@ class ::SpecialReply < ::Reply
end
end
test
"update other counters on parent destroy"
do
david
,
joanna
=
dog_lovers
(
:david
,
:joanna
)
assert_difference
'joanna.reload.dogs_count'
,
-
1
do
david
.
destroy
end
end
test
"reset the right counter if two have the same foreign key"
do
michael
=
people
(
:michael
)
assert_nothing_raised
(
ActiveRecord
::
StatementInvalid
)
do
...
...
activerecord/test/fixtures/dog_lovers.yml
浏览文件 @
66679c8e
...
...
@@ -2,3 +2,6 @@ david:
id
:
1
bred_dogs_count
:
0
trained_dogs_count
:
1
joanna
:
id
:
2
dogs_count
:
1
activerecord/test/fixtures/dogs.yml
浏览文件 @
66679c8e
sophie
:
id
:
1
trainer_id
:
1
dog_lover_id
:
2
activerecord/test/models/dog.rb
浏览文件 @
66679c8e
class
Dog
<
ActiveRecord
::
Base
belongs_to
:breeder
,
:class_name
=>
"DogLover"
,
:counter_cache
=>
:bred_dogs_count
belongs_to
:trainer
,
:class_name
=>
"DogLover"
,
:counter_cache
=>
:trained_dogs_count
belongs_to
:breeder
,
class_name:
"DogLover"
,
counter_cache: :bred_dogs_count
belongs_to
:trainer
,
class_name:
"DogLover"
,
counter_cache: :trained_dogs_count
belongs_to
:doglover
,
foreign_key: :dog_lover_id
,
class_name:
"DogLover"
,
counter_cache:
true
end
activerecord/test/models/dog_lover.rb
浏览文件 @
66679c8e
class
DogLover
<
ActiveRecord
::
Base
has_many
:trained_dogs
,
:class_name
=>
"Dog"
,
:foreign_key
=>
:trainer_id
has_many
:bred_dogs
,
:class_name
=>
"Dog"
,
:foreign_key
=>
:breeder_id
has_many
:trained_dogs
,
class_name:
"Dog"
,
foreign_key: :trainer_id
,
dependent: :destroy
has_many
:bred_dogs
,
class_name:
"Dog"
,
foreign_key: :breeder_id
has_many
:dogs
end
activerecord/test/schema/schema.rb
浏览文件 @
66679c8e
...
...
@@ -230,14 +230,16 @@ def create_table(*args, &block)
t
.
integer
:access_level
,
:default
=>
1
end
create_table
:dog_lovers
,
:force
=>
true
do
|
t
|
t
.
integer
:trained_dogs_count
,
:default
=>
0
t
.
integer
:bred_dogs_count
,
:default
=>
0
create_table
:dog_lovers
,
force:
true
do
|
t
|
t
.
integer
:trained_dogs_count
,
default:
0
t
.
integer
:bred_dogs_count
,
default:
0
t
.
integer
:dogs_count
,
default:
0
end
create_table
:dogs
,
:force
=>
true
do
|
t
|
t
.
integer
:trainer_id
t
.
integer
:breeder_id
t
.
integer
:dog_lover_id
end
create_table
:edges
,
:force
=>
true
,
:id
=>
false
do
|
t
|
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录