Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
773e45a5
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 搜索 >>
提交
773e45a5
编写于
1月 31, 2017
作者:
R
Rafael França
提交者:
GitHub
1月 31, 2017
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #27836 from kamipo/has_many_through_with_scope_should_respect_table_alias
Chain scope constraints should respect own table alias
上级
80dc3098
111ccc83
变更
6
显示空白变更内容
内联
并排
Showing
6 changed file
with
41 addition
and
5 deletion
+41
-5
activerecord/lib/active_record/associations/association_scope.rb
...ecord/lib/active_record/associations/association_scope.rb
+6
-5
activerecord/test/cases/associations/has_many_through_associations_test.rb
.../cases/associations/has_many_through_associations_test.rb
+14
-0
activerecord/test/models/family.rb
activerecord/test/models/family.rb
+4
-0
activerecord/test/models/family_tree.rb
activerecord/test/models/family_tree.rb
+4
-0
activerecord/test/models/user.rb
activerecord/test/models/user.rb
+4
-0
activerecord/test/schema/schema.rb
activerecord/test/schema/schema.rb
+9
-0
未找到文件。
activerecord/lib/active_record/associations/association_scope.rb
浏览文件 @
773e45a5
...
...
@@ -128,9 +128,9 @@ def add_constraints(scope, owner, association_klass, refl, chain_head, chain_tai
reflection
=
chain_head
while
reflection
table
=
reflection
.
alias_name
next_reflection
=
reflection
.
next
unless
reflection
==
chain_tail
next_reflection
=
reflection
.
next
foreign_table
=
next_reflection
.
alias_name
scope
=
next_chain_scope
(
scope
,
table
,
reflection
,
association_klass
,
foreign_table
,
next_reflection
)
end
...
...
@@ -138,7 +138,7 @@ def add_constraints(scope, owner, association_klass, refl, chain_head, chain_tai
# Exclude the scope of the association itself, because that
# was already merged in the #scope method.
reflection
.
constraints
.
each
do
|
scope_chain_item
|
item
=
eval_scope
(
reflection
.
klass
,
scope_chain_item
,
owner
)
item
=
eval_scope
(
reflection
.
klass
,
table
,
scope_chain_item
,
owner
)
if
scope_chain_item
==
refl
.
scope
scope
.
merge!
item
.
except
(
:where
,
:includes
)
...
...
@@ -153,14 +153,15 @@ def add_constraints(scope, owner, association_klass, refl, chain_head, chain_tai
scope
.
order_values
|=
item
.
order_values
end
reflection
=
reflection
.
next
reflection
=
next_reflection
end
scope
end
def
eval_scope
(
klass
,
scope
,
owner
)
klass
.
unscoped
.
instance_exec
(
owner
,
&
scope
)
def
eval_scope
(
klass
,
table
,
scope
,
owner
)
predicate_builder
=
PredicateBuilder
.
new
(
TableMetadata
.
new
(
klass
,
table
))
ActiveRecord
::
Relation
.
create
(
klass
,
table
,
predicate_builder
).
instance_exec
(
owner
,
&
scope
)
end
end
end
...
...
activerecord/test/cases/associations/has_many_through_associations_test.rb
浏览文件 @
773e45a5
...
...
@@ -28,6 +28,9 @@
require
"models/membership"
require
"models/club"
require
"models/organization"
require
"models/user"
require
"models/family"
require
"models/family_tree"
class
HasManyThroughAssociationsTest
<
ActiveRecord
::
TestCase
fixtures
:posts
,
:readers
,
:people
,
:comments
,
:authors
,
:categories
,
:taggings
,
:tags
,
...
...
@@ -1232,6 +1235,17 @@ def test_has_many_through_do_not_cache_association_reader_if_the_though_method_h
TenantMembership
.
current_member
=
nil
end
def
test_has_many_through_with_scope_should_respect_table_alias
family
=
Family
.
create!
users
=
3
.
times
.
map
{
User
.
create!
}
FamilyTree
.
create!
(
member:
users
[
0
],
family:
family
)
FamilyTree
.
create!
(
member:
users
[
1
],
family:
family
)
FamilyTree
.
create!
(
member:
users
[
2
],
family:
family
,
token:
"wat"
)
assert_equal
2
,
users
[
0
].
family_members
.
to_a
.
size
assert_equal
0
,
users
[
2
].
family_members
.
to_a
.
size
end
def
test_incorrectly_ordered_through_associations
assert_raises
(
ActiveRecord
::
HasManyThroughOrderError
)
do
DeveloperWithIncorrectlyOrderedHasManyThrough
.
create
(
...
...
activerecord/test/models/family.rb
0 → 100644
浏览文件 @
773e45a5
class
Family
<
ActiveRecord
::
Base
has_many
:family_trees
,
->
{
where
(
token:
nil
)
}
has_many
:members
,
through: :family_trees
end
activerecord/test/models/family_tree.rb
0 → 100644
浏览文件 @
773e45a5
class
FamilyTree
<
ActiveRecord
::
Base
belongs_to
:member
,
class_name:
"User"
,
foreign_key:
"member_id"
belongs_to
:family
end
activerecord/test/models/user.rb
浏览文件 @
773e45a5
...
...
@@ -7,6 +7,10 @@ class User < ActiveRecord::Base
has_and_belongs_to_many
:jobs_pool
,
class_name:
"Job"
,
join_table:
"jobs_pool"
has_one
:family_tree
,
->
{
where
(
token:
nil
)
},
foreign_key:
"member_id"
has_one
:family
,
through: :family_tree
has_many
:family_members
,
through: :family
,
source: :members
end
class
UserWithNotification
<
User
...
...
activerecord/test/schema/schema.rb
浏览文件 @
773e45a5
...
...
@@ -329,6 +329,15 @@
create_table
:eyes
,
force:
true
do
|
t
|
end
create_table
:families
,
force:
true
do
|
t
|
end
create_table
:family_trees
,
force:
true
do
|
t
|
t
.
references
:family
t
.
references
:member
t
.
string
:token
end
create_table
:funny_jokes
,
force:
true
do
|
t
|
t
.
string
:name
end
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录