You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: guides/source/active_storage_overview.md
+8-12Lines changed: 8 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -664,31 +664,27 @@ are stored before the form is submitted, they can be used to retain uploads when
664
664
665
665
## Querying
666
666
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.
668
668
669
669
### `has_one_attached`
670
670
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:
`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:
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.
0 commit comments