RAILS 6.1 supports multiple has_one_attached in ACTIVE STORAGE?

I have just updated RAILS to RAILS 6.1.

And I want to attach different PDF attachments onto one model.

And I was reading on another web page that RAILS now supported mutiple has_one_attached

statements in one model.

I don’t mean the has_many_attached !!!

In my usecase the model needs to attach a invoice and a credit advice.

So I have a model file looks like :

has_one_attached :invoice _paper, service: :upload_invoice_paper
has_one_attached :credit_paper, service: :upload_credit_paper

And the corresponding storage.yml :

service: Disk
root: <%= Rails.root.join("uploads/invoice_paper") %>

service: Disk
root: <%= Rails.root.join("uploads/credit_paper") %>

But when I try to run this code, I get :

ArgumentError (Cannot configure service :upload_credit_paper for OrderNow#credit_paper):

So it seems to be the second statement of has_one_attached will NOT be supported.

I’m not sure what the solution for your error is, but I know that having multiple has_one_attached statements is valid. In one app I wrote, a model has four. Your issue is related to the service used.

Hi Mark, thank you for your reply. Now I checked out again my “old” branch for that issue to test it again and it works ! :wink: Seems to be that I did not restart the RAILS server while I changed the storage.yml file.

1 Like