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

undefined method `attachment_reflections' for nil:NilClass when removing an entry with Active Storage with variants from console #40962

Closed
juansecaro opened this issue Dec 28, 2020 · 11 comments

Comments

@juansecaro
Copy link

juansecaro commented Dec 28, 2020

Steps to reproduce

Building a scaffold with an Active Storage image and at least 1 variant in a view throw this error when destroy.

In console it happens everytime, in the browser I have this error 1 time but I haven't been able to reproduce.

All images are still found after destroy in the system

Expected behavior

After detroy the image and its variants should be removed silently and without throwing any error.

Actual behavior

An error is thrown and all the images are still in the folder storage

rails_fails
rails_fails2

System configuration

Rails version: 6.1.0

Ruby version: 2.7.2

@josh-m-sharpe
Copy link
Contributor

josh-m-sharpe commented Dec 31, 2020

Ah - it's because you're using UUID primary keys and the ActiveStorage relations default to integers.

See this issue and my fix at the bottom: #40986

@josh-m-sharpe
Copy link
Contributor

undefined_method_`attachment_reflections__for_nil_NilClass_when_removing_an_entry_with_Active_Storage_with_variants_from_console_·_Issue__40962_·_rails_rails

@filipjakubowski
Copy link

filipjakubowski commented Dec 31, 2020

I had attachment_reflections errors for Nil class as well using rails 6.1. Error was not on deleting but processing variant.

gem 'rails', '~> 6.1.0'
gem 'image_processing', '~> 1.2'

updating imagemagic solved problem (MAC)
brew upgrade imagemagick ( or install if you have not done it before )

@juansecaro
Copy link
Author

I have this version (1.2) of image_processing and the problem keep going. It looks like @josh-m-sharpe is right and it's related to having UUID as a primary id. It's good to know that not using UUID is a work around, but I wonder if there is an easy fix to this.

@korkey128k
Copy link
Contributor

I've fixed this with a migration to remove the record_id column from active_storage_attachments and add it back with the :uuid option.

class ChangeActiveStorageRecordReferencesToUuid < ActiveRecord::Migration[6.1]
  def change
    remove_column :active_storage_attachments, :record_id
    add_column :active_storage_attachments, :record_id, :uuid, null: false
  end
end

You could also fix this by adding type: :uuid to the record_id reference in the initial migration.
This seems to work fine, but not fully confident it wouldn't have side effects. YMMV.

@juansecaro
Copy link
Author

I have decided to stick to default BigInt, but thanks for your help guys. Knowing the reason and an alternative solution is enough for me, but I don't feel 100% safe knowing it could be a source of pain and incompatibilities in the future win gems and integrations so I will take this time the safe road.

@rails-bot
Copy link

rails-bot bot commented Apr 2, 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 main, 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 Apr 2, 2021
@rails-bot rails-bot bot closed this as completed Apr 9, 2021
@ahanel13
Copy link

This is still an issue as of today
image

@zzak
Copy link
Member

zzak commented May 25, 2021

Related to #40986

@donmb1
Copy link

donmb1 commented Nov 30, 2021

add_column :active_storage_attachments, :record_id, :uuid, null: false

this solved it for me. But what if we have one model using ActiveStorage which has Integer as primary key and one has uuid? Seems like it cannot be mixed but its a common usecase.

@reinaldonunes
Copy link

I've fixed this with a migration to remove the record_id column from active_storage_attachments and add it back with the :uuid option.

class ChangeActiveStorageRecordReferencesToUuid < ActiveRecord::Migration[6.1]
  def change
    remove_column :active_storage_attachments, :record_id
    add_column :active_storage_attachments, :record_id, :uuid, null: false
  end
end

You could also fix this by adding type: :uuid to the record_id reference in the initial migration. This seems to work fine, but not fully confident it wouldn't have side effects. YMMV.

I had the same problem just now, I applied your resolution and it worked perfectly o/

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

No branches or pull requests

9 participants