Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
e7f39af3
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,发现更多精彩内容 >>
提交
e7f39af3
编写于
9月 20, 2017
作者:
E
Eileen M. Uchitelle
提交者:
GitHub
9月 20, 2017
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #30629 from yhirano55/add_scope_to_has_one_attached_macro
Add `with_attached_*` scope to `has_one_attached` macro
上级
057de4f7
704bf9b9
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
19 addition
and
0 deletion
+19
-0
activestorage/lib/active_storage/attached/macros.rb
activestorage/lib/active_storage/attached/macros.rb
+6
-0
activestorage/test/models/attachments_test.rb
activestorage/test/models/attachments_test.rb
+13
-0
未找到文件。
activestorage/lib/active_storage/attached/macros.rb
浏览文件 @
e7f39af3
...
@@ -12,6 +12,10 @@ module Attached::Macros
...
@@ -12,6 +12,10 @@ module Attached::Macros
# There is no column defined on the model side, Active Storage takes
# There is no column defined on the model side, Active Storage takes
# care of the mapping between your records and the attachment.
# care of the mapping between your records and the attachment.
#
#
# To avoid N+1 queries, you can include the attached blobs in your query like so:
#
# User.with_attached_avatar
#
# Under the covers, this relationship is implemented as a +has_one+ association to a
# Under the covers, this relationship is implemented as a +has_one+ association to a
# ActiveStorage::Attachment record and a +has_one-through+ association to a
# ActiveStorage::Attachment record and a +has_one-through+ association to a
# ActiveStorage::Blob record. These associations are available as +avatar_attachment+
# ActiveStorage::Blob record. These associations are available as +avatar_attachment+
...
@@ -33,6 +37,8 @@ def #{name}
...
@@ -33,6 +37,8 @@ def #{name}
has_one
:"
#{
name
}
_attachment"
,
->
{
where
(
name:
name
)
},
class_name:
"ActiveStorage::Attachment"
,
as: :record
has_one
:"
#{
name
}
_attachment"
,
->
{
where
(
name:
name
)
},
class_name:
"ActiveStorage::Attachment"
,
as: :record
has_one
:"
#{
name
}
_blob"
,
through: :"
#{
name
}
_attachment"
,
class_name:
"ActiveStorage::Blob"
,
source: :blob
has_one
:"
#{
name
}
_blob"
,
through: :"
#{
name
}
_attachment"
,
class_name:
"ActiveStorage::Blob"
,
source: :blob
scope
:"with_attached_
#{
name
}
"
,
->
{
includes
(
"
#{
name
}
_attachment"
:
:blob
)
}
if
dependent
==
:purge_later
if
dependent
==
:purge_later
before_destroy
{
public_send
(
name
).
purge_later
}
before_destroy
{
public_send
(
name
).
purge_later
}
end
end
...
...
activestorage/test/models/attachments_test.rb
浏览文件 @
e7f39af3
...
@@ -84,6 +84,19 @@ class ActiveStorage::AttachmentsTest < ActiveSupport::TestCase
...
@@ -84,6 +84,19 @@ class ActiveStorage::AttachmentsTest < ActiveSupport::TestCase
end
end
end
end
test
"find with attached blob"
do
records
=
%w[alice bob]
.
map
do
|
name
|
User
.
create!
(
name:
name
).
tap
do
|
user
|
user
.
avatar
.
attach
create_blob
(
filename:
"
#{
name
}
.jpg"
)
end
end
users
=
User
.
where
(
id:
records
.
map
(
&
:id
)).
with_attached_avatar
.
all
assert_equal
"alice.jpg"
,
users
.
first
.
avatar
.
filename
.
to_s
assert_equal
"bob.jpg"
,
users
.
second
.
avatar
.
filename
.
to_s
end
test
"attach existing blobs"
do
test
"attach existing blobs"
do
@user
.
highlights
.
attach
create_blob
(
filename:
"funky.jpg"
),
create_blob
(
filename:
"wonky.jpg"
)
@user
.
highlights
.
attach
create_blob
(
filename:
"funky.jpg"
),
create_blob
(
filename:
"wonky.jpg"
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录