draftsman icon indicating copy to clipboard operation
draftsman copied to clipboard

save_draft returns false on models having activestorage attachments

Open ahharvey opened this issue 6 years ago • 2 comments

I'm not sure whether this bug lies within Draftsman or my app, and I'm having trouble getting useful information to investigate further.

I'm posting here to check whether anyone else can replicate this.

A very simple test case:

# model
class User 
    has_drafts
    has_one_attached :avatar
end

# console

> u = User.last
> u.name = "New Name"
> u.save_draft
=> false

# 'u' has no errors
> u.errors.messages
=> {}

# if 'has_one_attached :avatar' is commented out a draft is created as expected
> u.save_draft
=> true
  1. Why would activestorage prevent Draftsman from saving drafts?
  2. What is the best way to inspect the save_draft method and find out what is causing it to fail?

I have also posted a question on SO https://stackoverflow.com/questions/54106001/why-does-draftsman-save-draft-return-false-when-the-model-has-activestorage-atta

ahharvey avatar Jan 09 '19 08:01 ahharvey

Draftsman is not designed to handle attachments. This probbably should be better documented.

See #41 for details.

jmfederico avatar Jan 09 '19 12:01 jmfederico

Thanks @jmfederico for quick response.

Turns out that this was caused by a silly oversight. My User model contained a legacy avatar db column from a previous attachment system. This was obviously causing conflict. Renaming the column has solved everything.

My biggest challenge was getting any information back about what was causing save_draft to return false. I ended up temporarily monkey patching Draftsman methods simply so I could insert breakpoints and dig around. Is there a better way of obtaining feedback from Draftsman when a draft fails to save?

Thanks!

PS - I don't know whether you've examined ActiveStorage, but it includes some fancy javascript to handle direct file uploads, and stores references to these remote files on a polymorphic child model. In other words, there is no need to deal with file blobs, and models only receives urls and other strings. I haven't looked into it, but I would think it's quite likely that Activestorage and Draftsman would play together much better than was previously possible with Paperclip, Carrierwave, etc..

ahharvey avatar Jan 09 '19 15:01 ahharvey