Skip to content

Commit 472a3de

Browse files
committed
improved ActiveStorage querying guides with PR feedback
1 parent 0bb56ec commit 472a3de

File tree

1 file changed

+8
-12
lines changed

1 file changed

+8
-12
lines changed

guides/source/active_storage_overview.md

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -664,31 +664,27 @@ are stored before the form is submitted, they can be used to retain uploads when
664664

665665
## Querying
666666

667-
Active Storage attachments are just ordinary Active Record associations behind the scenes, so you can use the usual `joins` / `where` toolbox to look up records whose attachments meet specific criteria.
667+
Active Storage attachments are Active Record associations behind the scenes, so you can use the usual [query methods](active_record_querying.html) to look up records for attachments that meet specific criteria.
668668

669669
### `has_one_attached`
670670

671-
`has_one_attached` creates a `has_one` association called `"<name>_attachment"` and a `has_one :through` association called `"<name>_blob"`.
672-
Need every user whose avatar is a PNG?
671+
[`has_one_attached`](https://api.rubyonrails.org/classes/ActiveStorage/Attached/Model.html#method-i-has_one_attached) creates a `has_one` association named `"<name>_attachment"` and a `has_one :through` association named `"<name>_blob"`.
672+
To select every user where the avatar is a PNG, run the following:
673673

674674
```ruby
675-
User
676-
.joins(:avatar_blob)
677-
.where(active_storage_blobs: { content_type: "image/png" })
675+
User.joins(:avatar_blob).where(active_storage_blobs: { content_type: "image/png" })
678676
```
679677

680678
### `has_many_attached`
681679

682-
`has_many_attached` creates a `has_many` association called `"<name>_attachments"` and a `has_many :through` association called `"<name>_blobs"` (note the plural).
683-
Want all messages whose images are videos instead of photos?
680+
[`has_many_attached`](https://api.rubyonrails.org/classes/ActiveStorage/Attached/Model.html#method-i-has_many_attached) creates a `has_many` association called `"<name>_attachments"` and a `has_many :through` association called `"<name>_blobs"` (note the plural).
681+
To select all messages where images are videos rather than photos you can do the following:
684682

685683
```ruby
686-
Message
687-
.joins(:images_blobs)
688-
.where(active_storage_blobs: { content_type: "video/mp4" })
684+
Message.joins(:images_blobs).where(active_storage_blobs: { content_type: "video/mp4" })
689685
```
690686

691-
Because these are plain SQL joins, remember that the query filters on the **`ActiveStorage::Blob`**, not the attachment record. You can combine the blob predicates above with any other scope conditions, just as you would with any other Active Record query.
687+
The query will filter on the [**`ActiveStorage::Blob`**](https://api.rubyonrails.org/classes/ActiveStorage/Blob.html), not the [attachment record](https://api.rubyonrails.org/classes/ActiveStorage/Attachment.html) because these are plain SQL joins. You can combine the blob predicates above with any other scope conditions, just as you would with any other Active Record query.
692688

693689

694690
Removing Files

0 commit comments

Comments
 (0)