Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
3b87c38d
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,发现更多精彩内容 >>
提交
3b87c38d
编写于
9月 26, 2011
作者:
J
Jon Leighton
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Fix belongs_to polymorphic with custom primary key on target.
Closes #3104.
上级
5c0e9333
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
30 addition
and
18 deletion
+30
-18
activerecord/lib/active_record/associations/association_scope.rb
...ecord/lib/active_record/associations/association_scope.rb
+6
-1
activerecord/lib/active_record/associations/belongs_to_association.rb
.../lib/active_record/associations/belongs_to_association.rb
+5
-1
activerecord/lib/active_record/reflection.rb
activerecord/lib/active_record/reflection.rb
+11
-15
activerecord/test/cases/associations/belongs_to_associations_test.rb
...d/test/cases/associations/belongs_to_associations_test.rb
+8
-0
activerecord/test/cases/reflection_test.rb
activerecord/test/cases/reflection_test.rb
+0
-1
未找到文件。
activerecord/lib/active_record/associations/association_scope.rb
浏览文件 @
3b87c38d
...
...
@@ -68,7 +68,12 @@ def add_constraints(scope)
end
if
reflection
.
source_macro
==
:belongs_to
key
=
reflection
.
association_primary_key
if
reflection
.
options
[
:polymorphic
]
key
=
reflection
.
association_primary_key
(
klass
)
else
key
=
reflection
.
association_primary_key
end
foreign_key
=
reflection
.
foreign_key
else
key
=
reflection
.
foreign_key
...
...
activerecord/lib/active_record/associations/belongs_to_association.rb
浏览文件 @
3b87c38d
...
...
@@ -45,7 +45,11 @@ def different_target?(record)
end
def
replace_keys
(
record
)
owner
[
reflection
.
foreign_key
]
=
record
&&
record
[
reflection
.
association_primary_key
]
if
record
owner
[
reflection
.
foreign_key
]
=
record
[
reflection
.
association_primary_key
(
record
.
class
)]
else
owner
[
reflection
.
foreign_key
]
=
nil
end
end
def
foreign_key_present?
...
...
activerecord/lib/active_record/reflection.rb
浏览文件 @
3b87c38d
...
...
@@ -211,11 +211,9 @@ def association_foreign_key
@association_foreign_key
||=
options
[
:association_foreign_key
]
||
class_name
.
foreign_key
end
def
association_primary_key
@association_primary_key
||=
options
[
:primary_key
]
||
!
options
[
:polymorphic
]
&&
klass
.
primary_key
||
'id'
# klass option is necessary to support loading polymorphic associations
def
association_primary_key
(
klass
=
self
.
klass
)
options
[
:primary_key
]
||
klass
.
primary_key
end
def
active_record_primary_key
...
...
@@ -463,17 +461,15 @@ def nested?
# We want to use the klass from this reflection, rather than just delegate straight to
# the source_reflection, because the source_reflection may be polymorphic. We still
# need to respect the source_reflection's :primary_key option, though.
def
association_primary_key
@association_primary_key
||=
begin
# Get the "actual" source reflection if the immediate source reflection has a
# source reflection itself
source_reflection
=
self
.
source_reflection
while
source_reflection
.
source_reflection
source_reflection
=
source_reflection
.
source_reflection
end
source_reflection
.
options
[
:primary_key
]
||
klass
.
primary_key
def
association_primary_key
(
klass
=
self
.
klass
)
# Get the "actual" source reflection if the immediate source reflection has a
# source reflection itself
source_reflection
=
self
.
source_reflection
while
source_reflection
.
source_reflection
source_reflection
=
source_reflection
.
source_reflection
end
source_reflection
.
options
[
:primary_key
]
||
klass
.
primary_key
end
# Gets an array of possible <tt>:through</tt> source reflection names:
...
...
activerecord/test/cases/associations/belongs_to_associations_test.rb
浏览文件 @
3b87c38d
...
...
@@ -13,6 +13,7 @@
require
'models/sponsor'
require
'models/member'
require
'models/essay'
require
'models/toy'
class
BelongsToAssociationsTest
<
ActiveRecord
::
TestCase
fixtures
:accounts
,
:companies
,
:developers
,
:projects
,
:topics
,
...
...
@@ -696,4 +697,11 @@ def test_self_referential_belongs_to_with_counter_cache_assigning_nil
assert_equal
nil
,
comment
.
reload
.
parent
assert_equal
0
,
comments
(
:greetings
).
reload
.
children_count
end
def
test_polymorphic_with_custom_primary_key
toy
=
Toy
.
create!
sponsor
=
Sponsor
.
create!
(
:sponsorable
=>
toy
)
assert_equal
toy
,
sponsor
.
reload
.
sponsorable
end
end
activerecord/test/cases/reflection_test.rb
浏览文件 @
3b87c38d
...
...
@@ -244,7 +244,6 @@ def test_association_primary_key
# Normal association
assert_equal
"id"
,
Author
.
reflect_on_association
(
:posts
).
association_primary_key
.
to_s
assert_equal
"name"
,
Author
.
reflect_on_association
(
:essay
).
association_primary_key
.
to_s
assert_equal
"id"
,
Tagging
.
reflect_on_association
(
:taggable
).
association_primary_key
.
to_s
# Through association (uses the :primary_key option from the source reflection)
assert_equal
"nick"
,
Author
.
reflect_on_association
(
:subscribers
).
association_primary_key
.
to_s
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录