Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
2c81e32a
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 搜索 >>
提交
2c81e32a
编写于
4月 16, 2020
作者:
R
Ryuta Kamizono
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Merge pull request #38929 from kamipo/fix_unscoping_association_scope
Fix unscoping association scope on joins not to raise an error
上级
f2f7bcc0
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
17 addition
and
12 deletion
+17
-12
activerecord/lib/active_record/reflection.rb
activerecord/lib/active_record/reflection.rb
+7
-5
activerecord/test/cases/associations/has_many_associations_test.rb
...ord/test/cases/associations/has_many_associations_test.rb
+6
-6
activerecord/test/models/car.rb
activerecord/test/models/car.rb
+4
-1
未找到文件。
activerecord/lib/active_record/reflection.rb
浏览文件 @
2c81e32a
...
...
@@ -183,20 +183,22 @@ def join_scope(table, foreign_table, foreign_klass)
scope_chain_items
=
join_scopes
(
table
,
predicate_builder
)
klass_scope
=
klass_join_scope
(
table
,
predicate_builder
)
if
type
klass_scope
.
where!
(
type
=>
foreign_klass
.
polymorphic_name
)
end
scope_chain_items
.
inject
(
klass_scope
,
&
:merge!
)
key
=
join_keys
.
key
foreign_key
=
join_keys
.
foreign_key
klass_scope
.
where!
(
table
[
key
].
eq
(
foreign_table
[
foreign_key
]))
if
type
klass_scope
.
where!
(
type
=>
foreign_klass
.
polymorphic_name
)
end
if
klass
.
finder_needs_type_condition?
klass_scope
.
where!
(
klass
.
send
(
:type_condition
,
table
))
end
scope_chain_items
.
inject
(
klass_scope
,
&
:merge!
)
klass_scope
end
def
join_scopes
(
table
,
predicate_builder
)
# :nodoc:
...
...
activerecord/test/cases/associations/has_many_associations_test.rb
浏览文件 @
2c81e32a
...
...
@@ -2657,10 +2657,11 @@ def test_association_with_rewhere_doesnt_set_inverse_instance_key
assert_equal
[
bulb1
],
car
.
bulbs
assert_equal
[
bulb1
,
bulb2
],
car
.
all_bulbs
.
sort_by
(
&
:id
)
assert_equal
[
bulb1
,
bulb2
],
Car
.
includes
(
:all_bulbs
).
find
(
car
.
id
).
all_bulbs
.
sort_by
(
&
:id
)
assert_equal
[
bulb1
,
bulb2
],
Car
.
eager_load
(
:all_bulbs
).
find
(
car
.
id
).
all_bulbs
.
sort_by
(
&
:id
)
end
test
"can unscope and where the default scope of the associated model"
do
Car
.
has_many
:other_bulbs
,
->
{
unscope
(
where:
[
:name
]).
where
(
name:
"other"
)
},
class_name:
"Bulb"
car
=
Car
.
create!
bulb1
=
Bulb
.
create!
name:
"defaulty"
,
car:
car
bulb2
=
Bulb
.
create!
name:
"other"
,
car:
car
...
...
@@ -2670,7 +2671,6 @@ def test_association_with_rewhere_doesnt_set_inverse_instance_key
end
test
"can rewhere the default scope of the associated model"
do
Car
.
has_many
:old_bulbs
,
->
{
rewhere
(
name:
"old"
)
},
class_name:
"Bulb"
car
=
Car
.
create!
bulb1
=
Bulb
.
create!
name:
"defaulty"
,
car:
car
bulb2
=
Bulb
.
create!
name:
"old"
,
car:
car
...
...
@@ -2681,11 +2681,11 @@ def test_association_with_rewhere_doesnt_set_inverse_instance_key
test
"unscopes the default scope of associated model when used with include"
do
car
=
Car
.
create!
bulb
=
Bulb
.
create!
name:
"other"
,
car:
car
bulb1
=
Bulb
.
create!
name:
"defaulty"
,
car:
car
bulb2
=
Bulb
.
create!
name:
"other"
,
car:
car
assert_equal
[
bulb
],
Car
.
find
(
car
.
id
).
all_bulbs
assert_equal
[
bulb
],
Car
.
includes
(
:all_bulbs
).
find
(
car
.
id
).
all_bulbs
assert_equal
[
bulb
],
Car
.
eager_load
(
:all_bulbs
).
find
(
car
.
id
).
all_bulbs
assert_equal
[
bulb1
,
bulb2
],
Car
.
includes
(
:all_bulbs2
).
find
(
car
.
id
).
all_bulbs2
.
sort_by
(
&
:id
)
assert_equal
[
bulb1
,
bulb2
],
Car
.
eager_load
(
:all_bulbs2
).
find
(
car
.
id
).
all_bulbs2
.
sort_by
(
&
:id
)
end
test
"raises RecordNotDestroyed when replaced child can't be destroyed"
do
...
...
activerecord/test/models/car.rb
浏览文件 @
2c81e32a
...
...
@@ -2,7 +2,10 @@
class
Car
<
ActiveRecord
::
Base
has_many
:bulbs
has_many
:all_bulbs
,
->
{
unscope
where: :name
},
class_name:
"Bulb"
has_many
:all_bulbs
,
->
{
unscope
(
where: :name
)
},
class_name:
"Bulb"
has_many
:all_bulbs2
,
->
{
unscope
(
:where
)
},
class_name:
"Bulb"
has_many
:other_bulbs
,
->
{
unscope
(
where: :name
).
where
(
name:
"other"
)
},
class_name:
"Bulb"
has_many
:old_bulbs
,
->
{
rewhere
(
name:
"old"
)
},
class_name:
"Bulb"
has_many
:funky_bulbs
,
class_name:
"FunkyBulb"
,
dependent: :destroy
has_many
:failed_bulbs
,
class_name:
"FailedBulb"
,
dependent: :destroy
has_many
:foo_bulbs
,
->
{
where
(
name:
"foo"
)
},
class_name:
"Bulb"
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录