Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: ActiveStorage method "attach" not work if model have validation errors #40333

Closed
nPaul opened this issue Oct 5, 2020 · 4 comments
Closed
Labels

Comments

@nPaul
Copy link

nPaul commented Oct 5, 2020

please fix it

example:

avatar = { io: StringIO.new(Base64.decode64(photo)), filename: "avatar" }
employee = Employee.new name: 'name'
employee.save!(validate: false) # have validation errors
puts employee.persisted?
true
res = employee.avatar.attach(avatar)
puts res
false

puts employee.errors.messages
{:chief=>["must exist"]}
employee.reload
puts employee.avatar.attached?
false
@abhaynikam
Copy link
Contributor

@nPaul Active Storage attach tries to re-save the Active Record. Refer: https://github.com/rails/rails/blob/master/activestorage/lib/active_storage/attached/one.rb#L33

Not sure if attach should accept the argument like validate: false to resolve this issue so not trying to raise a fix for it. But in case that is approved by some Rails team member I can raise a patch for it.

The work-around could be as follows:

avatar = { io: StringIO.new(Base64.decode64(photo)), filename: "avatar" }
employee = Employee.new name: 'name'
employee.avatar.attach(avatar)
employee.save!(validate: false)

@nPaul
Copy link
Author

nPaul commented Oct 6, 2020

@nPaul Active Storage attach tries to re-save the Active Record. Refer: https://github.com/rails/rails/blob/master/activestorage/lib/active_storage/attached/one.rb#L33

Not sure if attach should accept the argument like validate: false to resolve this issue so not trying to raise a fix for it. But in case that is approved by some Rails team member I can raise a patch for it.

The work-around could be as follows:

avatar = { io: StringIO.new(Base64.decode64(photo)), filename: "avatar" }
employee = Employee.new name: 'name'
employee.avatar.attach(avatar)
employee.save!(validate: false)

Yes it works

But i think it is bug, or not obviously

For example
I have more records with no valid records, created long ago time.
Now i want attach them avatars, but i cant do it.
employee = Employee.find(id) employee.avatar.attach(avatar)

i think method

must using save!(validate: false)
as default
he shouldn't care about valid or invalid model

@rails-bot
Copy link

rails-bot bot commented Jan 4, 2021

This issue has been automatically marked as stale because it has not been commented on for at least three months.
The resources of the Rails team are limited, and so we are asking for your help.
If you can still reproduce this error on the 6-1-stable branch or on master, please reply with all of the information you have about it in order to keep the issue open.
Thank you for all your contributions.

@rails-bot rails-bot bot added the stale label Jan 4, 2021
@rails-bot rails-bot bot closed this as completed Jan 11, 2021
@khalilgharbaoui
Copy link

+1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants