Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
87a8c611
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 搜索 >>
提交
87a8c611
编写于
3月 31, 2019
作者:
R
Ryuta Kamizono
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Merge pull request #35799 from leboshi/master
Fix callbacks on has_many :through associations
上级
724e1f4e
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
30 addition
and
11 deletion
+30
-11
activerecord/CHANGELOG.md
activerecord/CHANGELOG.md
+8
-0
activerecord/lib/active_record/associations/has_many_through_association.rb
...ctive_record/associations/has_many_through_association.rb
+4
-11
activerecord/test/cases/associations/has_many_through_associations_test.rb
.../cases/associations/has_many_through_associations_test.rb
+18
-0
未找到文件。
activerecord/CHANGELOG.md
浏览文件 @
87a8c611
*
Assign all attributes before calling
`build`
to ensure the child record is visible in
`before_add`
and
`after_add`
callbacks for
`has_many :through`
associations.
Fixes #33249.
*Ryan H. Kerr*
## Rails 5.2.3 (March 27, 2019) ##
*
Fix different
`count`
calculation when using
`size`
with manual
`select`
with DISTINCT.
...
...
activerecord/lib/active_record/associations/has_many_through_association.rb
浏览文件 @
87a8c611
...
...
@@ -57,21 +57,14 @@ def build_through_record(record)
@through_records
[
record
.
object_id
]
||=
begin
ensure_mutable
through_record
=
through_association
.
build
(
*
options_for_through_record
)
through_record
.
send
(
"
#{
source_reflection
.
name
}
="
,
record
)
attributes
=
through_scope_attributes
attributes
[
source_reflection
.
name
]
=
record
attributes
[
source_reflection
.
foreign_type
]
=
options
[
:source_type
]
if
options
[
:source_type
]
if
options
[
:source_type
]
through_record
.
send
(
"
#{
source_reflection
.
foreign_type
}
="
,
options
[
:source_type
])
end
through_record
through_association
.
build
(
attributes
)
end
end
def
options_for_through_record
[
through_scope_attributes
]
end
def
through_scope_attributes
scope
.
where_values_hash
(
through_association
.
reflection
.
name
.
to_s
).
except!
(
through_association
.
reflection
.
foreign_key
,
...
...
activerecord/test/cases/associations/has_many_through_associations_test.rb
浏览文件 @
87a8c611
...
...
@@ -1447,6 +1447,24 @@ def self.name; "PostWithNestedHasManyThrough"; end
assert_equal
[
subscription2
],
post
.
subscriptions
.
to_a
end
def
test_child_is_visible_to_join_model_in_add_association_callbacks
[
:before_add
,
:after_add
].
each
do
|
callback_name
|
sentient_treasure
=
Class
.
new
(
Treasure
)
do
def
self
.
name
;
"SentientTreasure"
;
end
has_many
:pet_treasures
,
foreign_key: :treasure_id
,
callback_name
=>
:check_pet!
has_many
:pets
,
through: :pet_treasures
def
check_pet!
(
added
)
raise
"No pet!"
if
added
.
pet
.
nil?
end
end
treasure
=
sentient_treasure
.
new
assert_nothing_raised
{
treasure
.
pets
<<
pets
(
:mochi
)
}
end
end
private
def
make_model
(
name
)
Class
.
new
(
ActiveRecord
::
Base
)
{
define_singleton_method
(
:name
)
{
name
}
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录