Skip to content

Commit

Permalink
add has_one
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianthedev committed Jul 4, 2023
1 parent 55e19c2 commit 0c31f14
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

Because [Active Storage](https://edgeguides.rubyonrails.org/active_storage_overview.html) is implemented as an association.

## `has_many`

For that you should create an `attachment_resource.rb` and configure it like so.

```ruby
Expand Down Expand Up @@ -30,6 +32,24 @@ class ProjectResource < Avo::BaseResource
end
```

![](/assets/img/recipes/use-the-has-many-field-for-active-storage-attachments/preview.png)
![](/assets/img/recipes/use-active-storage-attachments-as-associations/has_many.png)

We suggest making this resource read-only using Pundit policies so you don't accidentaly update Active Storage keys and settings.

## `has_many`

We can use the same resource on a different `has_one` association.

```ruby
class Post < ApplicationRecord
has_one_attached :cover_photo
end

class PostResource < Avo::BaseResource
# Please make sure you use the _attachment suffix.
# So the :cover_photo key from the model file becomes :cover_photo_attachment on the resource file.
field :cover_photo_attachment, as: :has_one
end
```

![](/assets/img/recipes/use-active-storage-attachments-as-associations/has_one.png)
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 0c31f14

Please sign in to comment.