Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
41d51b07
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 搜索 >>
提交
41d51b07
编写于
7月 18, 2017
作者:
S
Sean Griffin
提交者:
GitHub
7月 18, 2017
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #29834 from kamipo/fix_unscoping_default_scope_with_sti_association
Fix unscoping `default_scope` in STI associations
上级
8ebe1f2f
9aa04315
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
39 addition
and
3 deletion
+39
-3
activerecord/lib/active_record/associations/preloader/association.rb
...d/lib/active_record/associations/preloader/association.rb
+11
-1
activerecord/lib/active_record/reflection.rb
activerecord/lib/active_record/reflection.rb
+4
-2
activerecord/lib/active_record/relation.rb
activerecord/lib/active_record/relation.rb
+4
-0
activerecord/test/cases/scoping/default_scoping_test.rb
activerecord/test/cases/scoping/default_scoping_test.rb
+18
-0
activerecord/test/models/comment.rb
activerecord/test/models/comment.rb
+1
-0
activerecord/test/schema/schema.rb
activerecord/test/schema/schema.rb
+1
-0
未找到文件。
activerecord/lib/active_record/associations/preloader/association.rb
浏览文件 @
41d51b07
...
...
@@ -114,8 +114,18 @@ def reflection_scope
@reflection_scope
||=
reflection
.
scope_for
(
klass
)
end
def
klass_scope
current_scope
=
klass
.
current_scope
if
current_scope
&&
current_scope
.
empty_scope?
klass
.
unscoped
else
klass
.
default_scoped
end
end
def
build_scope
scope
=
klass
.
default_scoped
scope
=
klass
_scope
if
reflection
.
type
scope
.
where!
(
reflection
.
type
=>
model
.
base_class
.
sti_name
)
...
...
activerecord/lib/active_record/reflection.rb
浏览文件 @
41d51b07
...
...
@@ -219,8 +219,10 @@ def join_scopes(table, predicate_builder) # :nodoc:
end
def
klass_join_scope
(
table
,
predicate_builder
)
# :nodoc:
if
klass
.
current_scope
&&
klass
.
current_scope
.
values
.
empty?
klass
.
unscoped
current_scope
=
klass
.
current_scope
if
current_scope
&&
current_scope
.
empty_scope?
build_scope
(
table
,
predicate_builder
)
else
klass
.
default_scoped
(
build_scope
(
table
,
predicate_builder
))
end
...
...
activerecord/lib/active_record/relation.rb
浏览文件 @
41d51b07
...
...
@@ -642,6 +642,10 @@ def inspect
"#<
#{
self
.
class
.
name
}
[
#{
entries
.
join
(
', '
)
}
]>"
end
def
empty_scope?
# :nodoc:
@values
==
klass
.
unscoped
.
values
end
protected
def
load_records
(
records
)
...
...
activerecord/test/cases/scoping/default_scoping_test.rb
浏览文件 @
41d51b07
...
...
@@ -392,6 +392,24 @@ def test_unscoped_with_joins_should_not_have_default_scope
Comment
.
joins
(
:post
).
to_a
end
def
test_sti_association_with_unscoped_not_affected_by_default_scope
post
=
posts
(
:thinking
)
comments
=
[
comments
(
:does_it_hurt
)]
post
.
special_comments
.
update_all
(
deleted_at:
Time
.
now
)
assert_raises
(
ActiveRecord
::
RecordNotFound
)
{
Post
.
joins
(
:special_comments
).
find
(
post
.
id
)
}
assert_equal
[],
post
.
special_comments
SpecialComment
.
unscoped
do
assert_equal
post
,
Post
.
joins
(
:special_comments
).
find
(
post
.
id
)
assert_equal
comments
,
Post
.
joins
(
:special_comments
).
find
(
post
.
id
).
special_comments
assert_equal
comments
,
Post
.
eager_load
(
:special_comments
).
find
(
post
.
id
).
special_comments
assert_equal
comments
,
Post
.
includes
(
:special_comments
).
find
(
post
.
id
).
special_comments
assert_equal
comments
,
Post
.
preload
(
:special_comments
).
find
(
post
.
id
).
special_comments
end
end
def
test_default_scope_select_ignored_by_aggregations
assert_equal
DeveloperWithSelect
.
all
.
to_a
.
count
,
DeveloperWithSelect
.
count
end
...
...
activerecord/test/models/comment.rb
浏览文件 @
41d51b07
...
...
@@ -54,6 +54,7 @@ def to_s
end
class
SpecialComment
<
Comment
default_scope
{
where
(
deleted_at:
nil
)
}
end
class
SubSpecialComment
<
SpecialComment
...
...
activerecord/test/schema/schema.rb
浏览文件 @
41d51b07
...
...
@@ -189,6 +189,7 @@
t
.
string
:resource_id
t
.
string
:resource_type
t
.
integer
:developer_id
t
.
datetime
:deleted_at
end
create_table
:companies
,
force:
true
do
|
t
|
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录