Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
98e69078
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 搜索 >>
提交
98e69078
编写于
4月 03, 2014
作者:
J
Jan Habermann
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Properly handle scoping with has_many :through. Fixes #14537.
上级
165d93b2
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
25 addition
and
2 deletion
+25
-2
activerecord/lib/active_record/associations/through_association.rb
...ord/lib/active_record/associations/through_association.rb
+7
-2
activerecord/test/cases/associations/has_many_through_associations_test.rb
.../cases/associations/has_many_through_associations_test.rb
+12
-0
activerecord/test/models/post.rb
activerecord/test/models/post.rb
+4
-0
activerecord/test/models/reader.rb
activerecord/test/models/reader.rb
+2
-0
未找到文件。
activerecord/lib/active_record/associations/through_association.rb
浏览文件 @
98e69078
...
...
@@ -14,9 +14,14 @@ module ThroughAssociation #:nodoc:
def
target_scope
scope
=
super
chain
.
drop
(
1
).
each
do
|
reflection
|
relation
=
if
reflection
.
scope
reflection
.
klass
.
all
.
instance_eval
(
&
reflection
.
scope
)
else
reflection
.
klass
.
all
end
scope
.
merge!
(
reflection
.
klass
.
all
.
except
(
:select
,
:create_with
,
:includes
,
:preload
,
:joins
,
:eager_load
)
relation
.
except
(
:select
,
:create_with
,
:includes
,
:preload
,
:joins
,
:eager_load
)
)
end
scope
...
...
activerecord/test/cases/associations/has_many_through_associations_test.rb
浏览文件 @
98e69078
...
...
@@ -1105,4 +1105,16 @@ def test_has_many_through_obeys_order_on_through_association
def
test_has_many_through_with_includes_in_through_association_scope
assert_not_empty
posts
(
:welcome
).
author_address_extra_with_address
end
def
test_has_many_through_unscope_default_scope
post
=
Post
.
create!
(
:title
=>
'Beaches'
,
:body
=>
"I like beaches!"
)
Reader
.
create!
:person
=>
people
(
:david
),
:post
=>
post
LazyReader
.
create!
:person
=>
people
(
:susan
),
:post
=>
post
assert_equal
2
,
post
.
people
.
to_a
.
size
assert_equal
1
,
post
.
lazy_people
.
to_a
.
size
assert_equal
2
,
post
.
lazy_readers_unscope_skimmers
.
to_a
.
size
assert_equal
2
,
post
.
lazy_people_unscope_skimmers
.
to_a
.
size
end
end
activerecord/test/models/post.rb
浏览文件 @
98e69078
...
...
@@ -144,6 +144,10 @@ def add_joins_and_select
has_many
:lazy_readers
has_many
:lazy_readers_skimmers_or_not
,
->
{
where
(
skimmer:
[
true
,
false
])
},
:class_name
=>
'LazyReader'
has_many
:lazy_people
,
:through
=>
:lazy_readers
,
:source
=>
:person
has_many
:lazy_readers_unscope_skimmers
,
->
{
skimmers_or_not
},
:class_name
=>
'LazyReader'
has_many
:lazy_people_unscope_skimmers
,
:through
=>
:lazy_readers_unscope_skimmers
,
:source
=>
:person
def
self
.
top
(
limit
)
ranked_by_comments
.
limit_by
(
limit
)
...
...
activerecord/test/models/reader.rb
浏览文件 @
98e69078
...
...
@@ -15,6 +15,8 @@ class SecureReader < ActiveRecord::Base
class
LazyReader
<
ActiveRecord
::
Base
self
.
table_name
=
"readers"
default_scope
->
{
where
(
skimmer:
true
)
}
scope
:skimmers_or_not
,
->
{
unscope
(
:where
=>
:skimmer
)
}
belongs_to
:post
belongs_to
:person
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录