rails_admin: Active Storage: `method_missing': undefined method `has_one_attached'
Hello guys, I’m facing a issue while using active storage with rails_admin.
What’s happening is that I’ve model called Product which should have a image. I’ve installed active storage by running the following commands:
rails active_storage:install
db:migrate
After that, I’ve added a new attibute on my Model as:
has_one_attached :image
However, rails does not recognize it as a method:
class:Product’ /Users/username/.rvm/gems/ruby-2.5.1/gems/activerecord-5.2.0/lib/active_record/dynamic_matchers.rb:22:in
method_missing': undefined methodhas_one_attached’ for Product (call ‘Product.connection’ to establish a connection):Class (NoMethodError)
I’ve created a application from the scratch without rails_admin and it worked fine.
Note: I’m using rails 5.2.0 and ruby 2.5.1
Any ideas?
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 7
- Comments: 22
Commits related to this issue
- Reload configuration on change in development mode Fixes #2726, Fixes #3025 — committed to railsadminteam/rails_admin by mshibuya 3 years ago
- Reload configuration on change in development mode Fixes #2726, Fixes #3025 — committed to railsadminteam/rails_admin by mshibuya 3 years ago
- Reload configuration on change in development mode Fixes #2726, Fixes #3025 — committed to railsadminteam/rails_admin by mshibuya 3 years ago
I think I’ve had the same issue. I was trying to configure model inside rails_admin initializer using
config.model Product do ... end. Instead it should’ve beenconfig.model 'Product' do ... end. The model name must be quoted.At least up to 5.2.4.3, ActiveStorage seems to initialize and inject its methods into ActiveRecord after Rails config/initializers are loaded. If any initializer uses any model calling ActiveStorage macros, as
has_one_attachedorhas_many_attached, those methods are missing by then.For example, if you have a User model using
has_one_attached :image, and you use User as your Devise model, Device initializer will refer User, and when User is loaded the methodhas_one_attachedwill be missing.I added this code, taken from
active_storage/engine, at the end ofconfig/application.rb, and it worked for me.Initalizers are run before your application code runs. Models are interpreted only when needed (on a firs request perhaps). But if you reference a model in the initializer using a constant ie.
config.model Product do ... end, the model has to be interpreted before the rest of your initializer code and other initalizers. Configuring your models using stringconfig.model 'Product' do ... endworks well with rails_admin.It seems that
ActiveStoragehas its own initalizer which waitsActiveRecordto load before it inserts its macros. Perhapsrails_admin.rbinitalizer is executed beforeactive_storage.attachedinitalizer ?declaring this on model solved the problem for me
` class User < ApplicationRecord has_one_attached :avatar
end `
reference https://github.com/sferik/rails_admin/issues/2990#issuecomment-378220331
thanks a lot @dmilisic, your solution to use a string instead of a constant helped me a lot!!
I ignored this thread because I thought it would not solve my issue. Turns out I’m completely wrong. Thank you so much! @dmilisic
@dmilisic https://github.com/sferik/rails_admin/issues/3025#issuecomment-392513722 Thanks! Works for me. You did my day.
@dmilisic thank you for the support! We have managed to solve the problem. The
Subscriptionmodel was calling a constant defined in theUsermodel and then make theUserbe load beforehas_one_attachedcreation (I think!?). From the previously stacktrace:So we just copy the constant to the
Subscriptionclass and then it worked.