Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
5d8bb060
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,发现更多精彩内容 >>
提交
5d8bb060
编写于
10月 13, 2010
作者:
J
Jon Leighton
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Refactoring ThroughAssociationScope#construct_through_joins
上级
781ad0f8
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
62 addition
and
54 deletion
+62
-54
activerecord/lib/active_record/associations/through_association_scope.rb
...b/active_record/associations/through_association_scope.rb
+62
-54
未找到文件。
activerecord/lib/active_record/associations/through_association_scope.rb
浏览文件 @
5d8bb060
...
...
@@ -64,8 +64,7 @@ def construct_through_joins
# Iterate over each pair in the through reflection chain, joining them together
@reflection
.
through_reflection_chain
.
each_cons
(
2
)
do
|
left
,
right
|
polymorphic_join
=
nil
left_table
,
right_table
=
table_aliases
[
left
],
table_aliases
[
right
]
right_table_and_alias
=
table_name_and_alias
(
right
.
quoted_table_name
,
table_aliases
[
right
])
if
left
.
source_reflection
.
nil?
# TODO: Perhaps need to pay attention to left.options[:primary_key] and
...
...
@@ -73,75 +72,56 @@ def construct_through_joins
case
left
.
macro
when
:belongs_to
left_primary_key
=
left
.
klass
.
primary_key
right_primary_key
=
left
.
primary_key_name
joins
<<
inner_join_sql
(
right_table_and_alias
,
table_aliases
[
left
],
left
.
klass
.
primary_key
,
table_aliases
[
right
],
left
.
primary_key_name
)
when
:has_many
,
:has_one
left_primary_key
=
left
.
primary_key_name
right_primary_key
=
right
.
klass
.
primary_key
if
left
.
options
[
:as
]
polymorphic_join
=
"AND %s.%s = %s"
%
[
table_aliases
[
left
],
"
#{
left
.
options
[
:as
]
}
_type"
,
# TODO: Why right.klass.name? Rather than left.active_record.name?
# TODO: Also should maybe use the base_class (see related code in JoinAssociation)
@owner
.
class
.
quote_value
(
right
.
klass
.
name
)
]
end
joins
<<
inner_join_sql
(
right_table_and_alias
,
table_aliases
[
left
],
left
.
primary_key_name
,
table_aliases
[
right
],
right
.
klass
.
primary_key
,
polymorphic_conditions
(
left
,
left
.
options
[
:as
])
)
when
:has_and_belongs_to_many
raise
NotImplementedError
end
else
case
left
.
source_reflection
.
macro
when
:belongs_to
left_primary_key
=
left
.
klass
.
primary_key
right_primary_key
=
left
.
source_reflection
.
primary_key_name
if
left
.
options
[
:source_type
]
polymorphic_join
=
"AND %s.%s = %s"
%
[
table_aliases
[
right
],
left
.
source_reflection
.
options
[
:foreign_type
].
to_s
,
@owner
.
class
.
quote_value
(
left
.
options
[
:source_type
])
]
end
joins
<<
inner_join_sql
(
right_table_and_alias
,
table_aliases
[
left
],
left
.
klass
.
primary_key
,
table_aliases
[
right
],
left
.
source_reflection
.
primary_key_name
,
source_type_conditions
(
left
)
)
when
:has_many
,
:has_one
left_primary_key
=
left
.
source_reflection
.
primary_key_name
right_primary_key
=
right
.
klass
.
primary_key
if
left
.
source_reflection
.
options
[
:as
]
polymorphic_join
=
"AND %s.%s = %s"
%
[
table_aliases
[
left
],
"
#{
left
.
source_reflection
.
options
[
:as
]
}
_type"
,
@owner
.
class
.
quote_value
(
right
.
klass
.
name
)
]
end
joins
<<
inner_join_sql
(
right_table_and_alias
,
table_aliases
[
left
],
left
.
source_reflection
.
primary_key_name
,
table_aliases
[
right
],
right
.
klass
.
primary_key
,
polymorphic_conditions
(
left
,
left
.
source_reflection
.
options
[
:as
])
)
when
:has_and_belongs_to_many
join_table
,
left_table
=
left_table
join_table
,
left_table
=
table_aliases
[
left
]
left_primary_key
=
left
.
klass
.
primary_key
join_primary_key
=
left
.
source_reflection
.
association_foreign_key
joins
<<
"INNER JOIN %s ON %s.%s = %s.%s"
%
[
joins
<<
inner_join_sql
(
table_name_and_alias
(
quote_table_name
(
left
.
source_reflection
.
options
[
:join_table
]),
join_table
),
left_table
,
left_primary_key
,
join_table
,
join_primary_key
]
left_table
=
join_table
left_table
,
left
.
klass
.
primary_key
,
join_table
,
left
.
source_reflection
.
association_foreign_key
)
left_primary_key
=
left
.
source_reflection
.
primary_key_name
right_primary_key
=
right
.
klass
.
primary_key
joins
<<
inner_join_sql
(
right_table_and_alias
,
join_table
,
left
.
source_reflection
.
primary_key_name
,
table_aliases
[
right
],
right
.
klass
.
primary_key
)
end
end
joins
<<
"INNER JOIN %s ON %s.%s = %s.%s %s"
%
[
table_name_and_alias
(
right
.
quoted_table_name
,
right_table
),
left_table
,
left_primary_key
,
right_table
,
right_primary_key
,
polymorphic_join
]
end
joins
.
join
(
" "
)
...
...
@@ -192,6 +172,34 @@ def quote_table_name(table_name)
def
table_name_and_alias
(
table_name
,
table_alias
)
"
#{
table_name
}
#{
table_alias
if
table_alias
!=
table_name
}
"
.
strip
end
def
inner_join_sql
(
table
,
on_left_table
,
on_left_key
,
on_right_table
,
on_right_key
,
conds
=
nil
)
"INNER JOIN %s ON %s.%s = %s.%s %s"
%
[
table
,
on_left_table
,
on_left_key
,
on_right_table
,
on_right_key
,
conds
]
end
def
polymorphic_conditions
(
reflection
,
interface_name
)
if
interface_name
"AND %s.%s = %s"
%
[
table_aliases
[
reflection
],
"
#{
interface_name
}
_type"
,
@owner
.
class
.
quote_value
(
reflection
.
active_record
.
base_class
.
name
)
]
end
end
def
source_type_conditions
(
reflection
)
if
reflection
.
options
[
:source_type
]
"AND %s.%s = %s"
%
[
table_aliases
[
reflection
.
through_reflection
],
reflection
.
source_reflection
.
options
[
:foreign_type
].
to_s
,
@owner
.
class
.
quote_value
(
reflection
.
options
[
:source_type
])
]
end
end
# Construct attributes for associate pointing to owner.
def
construct_owner_attributes
(
reflection
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录