Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
65f055a3
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,发现更多精彩内容 >>
提交
65f055a3
编写于
10月 05, 2009
作者:
E
Emilio Tagua
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Added eager loading support to Relation and ActiveRecord#all.
上级
3747f896
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
34 addition
and
21 deletion
+34
-21
activerecord/lib/active_record/associations.rb
activerecord/lib/active_record/associations.rb
+2
-2
activerecord/lib/active_record/base.rb
activerecord/lib/active_record/base.rb
+6
-11
activerecord/lib/active_record/relation.rb
activerecord/lib/active_record/relation.rb
+25
-7
activerecord/test/cases/associations/cascaded_eager_loading_test.rb
...rd/test/cases/associations/cascaded_eager_loading_test.rb
+1
-1
未找到文件。
activerecord/lib/active_record/associations.rb
浏览文件 @
65f055a3
...
...
@@ -1455,9 +1455,9 @@ def add_touch_callbacks(reflection, touch_attribute)
after_destroy
(
method_name
)
end
def
find_with_associations
(
options
=
{})
def
find_with_associations
(
options
=
{}
,
join_dependency
=
nil
)
catch
:invalid_query
do
join_dependency
=
JoinDependency
.
new
(
self
,
merge_includes
(
scope
(
:find
,
:include
),
options
[
:include
]),
options
[
:joins
])
join_dependency
||
=
JoinDependency
.
new
(
self
,
merge_includes
(
scope
(
:find
,
:include
),
options
[
:include
]),
options
[
:joins
])
rows
=
select_all_rows
(
options
,
join_dependency
)
return
join_dependency
.
instantiate
(
rows
)
end
...
...
activerecord/lib/active_record/base.rb
浏览文件 @
65f055a3
...
...
@@ -666,23 +666,18 @@ def last(*args)
def
all
(
*
args
)
options
=
args
.
extract_options!
if
options
.
empty?
&&
!
scoped?
(
:find
)
relation
=
arel_table
else
relation
=
construct_finder_arel
(
options
)
include_associations
=
merge_includes
(
scope
(
:find
,
:include
),
options
[
:include
])
# if include_associations.any? && references_eager_loaded_tables?(options)
# join_dependency = JoinDependency.new(self, include_associations, options[:joins])
# relation = construct_finder_arel_with_included_associations(options, join_dependency)
# relation.preload(include_associations)
# else
relation
=
construct_finder_arel
(
options
)
if
include_associations
.
any?
if
include_associations
.
any?
if
references_eager_loaded_tables?
(
options
)
relation
.
eager_load
(
include_associations
)
else
relation
.
preload
(
include_associations
)
#
end
end
end
end
relation
...
...
activerecord/lib/active_record/relation.rb
浏览文件 @
65f055a3
...
...
@@ -7,11 +7,17 @@ def initialize(klass, relation)
@klass
,
@relation
=
klass
,
relation
@readonly
=
false
@associations_to_preload
=
[]
@eager_load_associations
=
[]
end
def
preload
(
association
)
@associations_to_preload
<<
association
@associations_to_preload
.
flatten!
@associations_to_preload
+=
association
self
end
def
eager_load
(
association
)
@eager_load_associations
+=
association
self
end
def
readonly
...
...
@@ -20,11 +26,23 @@ def readonly
end
def
to_a
records
=
@klass
.
find_by_sql
(
@relation
.
to_sql
)
@klass
.
send
:preload_associations
,
records
,
@associations_to_preload
unless
@associations_to_preload
.
empty?
records
.
each
{
|
record
|
record
.
readonly!
}
if
@readonly
if
@eager_load_associations
.
any?
records
=
catch
:invalid_query
do
@klass
.
send
(
:find_with_associations
,
{
:select
=>
@relation
.
send
(
:select_clauses
).
join
(
', '
),
:joins
=>
@relation
.
joins
(
relation
),
:group
=>
@relation
.
send
(
:group_clauses
).
join
(
', '
),
:order
=>
@relation
.
send
(
:order_clauses
).
join
(
', '
),
:conditions
=>
@relation
.
send
(
:where_clauses
).
join
(
"
\n\t
AND "
),
:limit
=>
@relation
.
taken
},
ActiveRecord
::
Associations
::
ClassMethods
::
JoinDependency
.
new
(
@klass
,
@eager_load_associations
,
nil
))
end
else
records
=
@klass
.
find_by_sql
(
@relation
.
to_sql
)
@klass
.
send
(
:preload_associations
,
records
,
@associations_to_preload
)
unless
@associations_to_preload
.
empty?
records
.
each
{
|
record
|
record
.
readonly!
}
if
@readonly
end
records
end
...
...
activerecord/test/cases/associations/cascaded_eager_loading_test.rb
浏览文件 @
65f055a3
...
...
@@ -104,7 +104,7 @@ def test_eager_association_loading_of_stis_with_multiple_references
authors
.
first
.
posts
.
first
.
special_comments
.
first
.
post
.
very_special_comment
end
end
def
test_eager_association_loading_where_first_level_returns_nil
authors
=
Author
.
find
(
:all
,
:include
=>
{
:post_about_thinking
=>
:comments
},
:order
=>
'authors.id DESC'
)
assert_equal
[
authors
(
:mary
),
authors
(
:david
)],
authors
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录