Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
fa82ea40
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,发现更多精彩内容 >>
未验证
提交
fa82ea40
编写于
7月 29, 2020
作者:
E
Eugene Kenny
提交者:
GitHub
7月 29, 2020
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #39771 from eugeneius/autosave_exactly_once
Autosave collection associations exactly once
上级
e396bcaa
21d67f18
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
20 addition
and
9 deletion
+20
-9
activerecord/lib/active_record/autosave_association.rb
activerecord/lib/active_record/autosave_association.rb
+9
-9
activerecord/test/cases/autosave_association_test.rb
activerecord/test/cases/autosave_association_test.rb
+10
-0
activerecord/test/models/post.rb
activerecord/test/models/post.rb
+1
-0
未找到文件。
activerecord/lib/active_record/autosave_association.rb
浏览文件 @
fa82ea40
...
...
@@ -29,7 +29,7 @@ module ActiveRecord
# == Callbacks
#
# Association with autosave option defines several callbacks on your
# model (before_save, after_create, after_update). Please note that
# model (
around_save,
before_save, after_create, after_update). Please note that
# callbacks are executed in the order they were defined in
# model. You should avoid modifying the association content before
# autosave callbacks are executed. Placing your callbacks after
...
...
@@ -188,8 +188,7 @@ def add_autosave_association_callbacks(reflection)
save_method
=
:"autosave_associated_records_for_
#{
reflection
.
name
}
"
if
reflection
.
collection?
before_save
:before_save_collection_association
after_save
:after_save_collection_association
around_save
:around_save_collection_association
define_non_cyclic_method
(
save_method
)
{
save_collection_association
(
reflection
)
}
# Doesn't use after_save as that would save associations added in after_create/after_update twice
...
...
@@ -362,14 +361,15 @@ def normalize_reflection_attribute(indexed_attribute, reflection, index, attribu
end
end
# Is used as a
before
_save callback to check while saving a collection
# Is used as a
n around
_save callback to check while saving a collection
# association whether or not the parent was a new record before saving.
def
before
_save_collection_association
@new_record_before_save
||=
new_record?
end
def
around
_save_collection_association
previously_new_record_before_save
=
(
@new_record_before_save
||=
false
)
@new_record_before_save
=
!
previously_new_record_before_save
&&
new_record?
def
after_save_collection_association
@new_record_before_save
=
false
yield
ensure
@new_record_before_save
=
previously_new_record_before_save
end
# Saves any new associated records, or all loaded autosave associations if
...
...
activerecord/test/cases/autosave_association_test.rb
浏览文件 @
fa82ea40
...
...
@@ -6,6 +6,7 @@
require
"models/bird"
require
"models/post"
require
"models/comment"
require
"models/category"
require
"models/company"
require
"models/contract"
require
"models/customer"
...
...
@@ -820,6 +821,15 @@ def test_autosave_new_record_with_after_create_callback
assert_not_nil
post
.
author_id
end
def
test_autosave_new_record_with_after_create_callback_and_habtm_association
post
=
PostWithAfterCreateCallback
.
new
(
title:
"Captain Murphy"
,
body:
"is back"
)
post
.
comments
.
build
(
body:
"foo"
)
post
.
categories
.
build
(
name:
"bar"
)
post
.
save!
assert_equal
1
,
post
.
categories
.
reload
.
length
end
end
class
TestDestroyAsPartOfAutosaveAssociation
<
ActiveRecord
::
TestCase
...
...
activerecord/test/models/post.rb
浏览文件 @
fa82ea40
...
...
@@ -291,6 +291,7 @@ class PostWithAfterCreateCallback < ActiveRecord::Base
self
.
inheritance_column
=
:disabled
self
.
table_name
=
"posts"
has_many
:comments
,
foreign_key: :post_id
has_and_belongs_to_many
:categories
,
foreign_key: :post_id
after_create
do
|
post
|
update_attribute
(
:author_id
,
comments
.
first
.
id
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录