Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
ed7c851c
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 搜索 >>
提交
ed7c851c
编写于
9月 04, 2014
作者:
A
Aaron Patterson
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #16788 from codeodor/fix-16761
Skip StatementCache for eager loaded associations
上级
2e6625fb
4abbdbdf
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
34 addition
and
3 deletion
+34
-3
activerecord/lib/active_record/associations/collection_association.rb
.../lib/active_record/associations/collection_association.rb
+1
-1
activerecord/lib/active_record/associations/singular_association.rb
...rd/lib/active_record/associations/singular_association.rb
+1
-1
activerecord/lib/active_record/core.rb
activerecord/lib/active_record/core.rb
+1
-0
activerecord/test/cases/scoping/default_scoping_test.rb
activerecord/test/cases/scoping/default_scoping_test.rb
+20
-1
activerecord/test/models/comment.rb
activerecord/test/models/comment.rb
+5
-0
activerecord/test/models/post.rb
activerecord/test/models/post.rb
+6
-0
未找到文件。
activerecord/lib/active_record/associations/collection_association.rb
浏览文件 @
ed7c851c
...
@@ -407,7 +407,7 @@ def null_scope?
...
@@ -407,7 +407,7 @@ def null_scope?
private
private
def
get_records
def
get_records
return
scope
.
to_a
if
reflection
.
scope_chain
.
any?
(
&
:any?
)
return
scope
.
to_a
if
reflection
.
scope_chain
.
any?
(
&
:any?
)
||
scope
.
eager_loading?
conn
=
klass
.
connection
conn
=
klass
.
connection
sc
=
reflection
.
association_scope_cache
(
conn
,
owner
)
do
sc
=
reflection
.
association_scope_cache
(
conn
,
owner
)
do
...
...
activerecord/lib/active_record/associations/singular_association.rb
浏览文件 @
ed7c851c
...
@@ -39,7 +39,7 @@ def create_scope
...
@@ -39,7 +39,7 @@ def create_scope
end
end
def
get_records
def
get_records
return
scope
.
limit
(
1
).
to_a
if
reflection
.
scope_chain
.
any?
(
&
:any?
)
return
scope
.
limit
(
1
).
to_a
if
reflection
.
scope_chain
.
any?
(
&
:any?
)
||
scope
.
eager_loading?
conn
=
klass
.
connection
conn
=
klass
.
connection
sc
=
reflection
.
association_scope_cache
(
conn
,
owner
)
do
sc
=
reflection
.
association_scope_cache
(
conn
,
owner
)
do
...
...
activerecord/lib/active_record/core.rb
浏览文件 @
ed7c851c
...
@@ -152,6 +152,7 @@ def find(*ids)
...
@@ -152,6 +152,7 @@ def find(*ids)
def
find_by
(
*
args
)
def
find_by
(
*
args
)
return
super
if
current_scope
||
!
(
Hash
===
args
.
first
)
||
reflect_on_all_aggregations
.
any?
return
super
if
current_scope
||
!
(
Hash
===
args
.
first
)
||
reflect_on_all_aggregations
.
any?
return
super
if
default_scopes
.
any?
hash
=
args
.
first
hash
=
args
.
first
...
...
activerecord/test/cases/scoping/default_scoping_test.rb
浏览文件 @
ed7c851c
require
'cases/helper'
require
'cases/helper'
require
'models/post'
require
'models/post'
require
'models/comment'
require
'models/developer'
require
'models/developer'
class
DefaultScopingTest
<
ActiveRecord
::
TestCase
class
DefaultScopingTest
<
ActiveRecord
::
TestCase
fixtures
:developers
,
:posts
fixtures
:developers
,
:posts
,
:comments
def
test_default_scope
def
test_default_scope
expected
=
Developer
.
all
.
merge!
(
:order
=>
'salary DESC'
).
to_a
.
collect
{
|
dev
|
dev
.
salary
}
expected
=
Developer
.
all
.
merge!
(
:order
=>
'salary DESC'
).
to_a
.
collect
{
|
dev
|
dev
.
salary
}
...
@@ -378,6 +379,24 @@ def test_default_scope_include_with_count
...
@@ -378,6 +379,24 @@ def test_default_scope_include_with_count
assert_equal
1
,
DeveloperWithIncludes
.
where
(
:audit_logs
=>
{
:message
=>
'foo'
}).
count
assert_equal
1
,
DeveloperWithIncludes
.
where
(
:audit_logs
=>
{
:message
=>
'foo'
}).
count
end
end
def
test_default_scope_with_references_works_through_collection_association
post
=
PostWithCommentWithDefaultScopeReferencesAssociation
.
create!
(
title:
"Hello World"
,
body:
"Here we go."
)
comment
=
post
.
comment_with_default_scope_references_associations
.
create!
(
body:
"Great post."
,
developer_id:
Developer
.
first
.
id
)
assert_equal
comment
,
post
.
comment_with_default_scope_references_associations
.
to_a
.
first
end
def
test_default_scope_with_references_works_through_association
post
=
PostWithCommentWithDefaultScopeReferencesAssociation
.
create!
(
title:
"Hello World"
,
body:
"Here we go."
)
comment
=
post
.
comment_with_default_scope_references_associations
.
create!
(
body:
"Great post."
,
developer_id:
Developer
.
first
.
id
)
assert_equal
comment
,
post
.
first_comment
end
def
test_default_scope_with_references_works_with_find_by
post
=
PostWithCommentWithDefaultScopeReferencesAssociation
.
create!
(
title:
"Hello World"
,
body:
"Here we go."
)
comment
=
post
.
comment_with_default_scope_references_associations
.
create!
(
body:
"Great post."
,
developer_id:
Developer
.
first
.
id
)
assert_equal
comment
,
CommentWithDefaultScopeReferencesAssociation
.
find_by
(
id:
comment
.
id
)
end
unless
in_memory_db?
unless
in_memory_db?
def
test_default_scope_is_threadsafe
def
test_default_scope_is_threadsafe
threads
=
[]
threads
=
[]
...
...
activerecord/test/models/comment.rb
浏览文件 @
ed7c851c
...
@@ -52,3 +52,8 @@ class CommentThatAutomaticallyAltersPostBody < Comment
...
@@ -52,3 +52,8 @@ class CommentThatAutomaticallyAltersPostBody < Comment
comment
.
post
.
update_attributes
(
body:
"Automatically altered"
)
comment
.
post
.
update_attributes
(
body:
"Automatically altered"
)
end
end
end
end
class
CommentWithDefaultScopeReferencesAssociation
<
Comment
default_scope
->
{
includes
(
:developer
).
order
(
'developers.name'
).
references
(
:developer
)
}
belongs_to
:developer
end
activerecord/test/models/post.rb
浏览文件 @
ed7c851c
...
@@ -218,3 +218,9 @@ class PostThatLoadsCommentsInAnAfterSaveHook < ActiveRecord::Base
...
@@ -218,3 +218,9 @@ class PostThatLoadsCommentsInAnAfterSaveHook < ActiveRecord::Base
post
.
comments
.
load
post
.
comments
.
load
end
end
end
end
class
PostWithCommentWithDefaultScopeReferencesAssociation
<
ActiveRecord
::
Base
self
.
table_name
=
'posts'
has_many
:comment_with_default_scope_references_associations
,
foreign_key: :post_id
has_one
:first_comment
,
class_name:
"CommentWithDefaultScopeReferencesAssociation"
,
foreign_key: :post_id
end
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录