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

Active Storage: method_missing': undefined method has_one_attached' #3025

Closed
cristiansimioni opened this issue May 28, 2018 · 22 comments
Closed
Milestone

Comments

@cristiansimioni
Copy link

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 method has_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?

@dmilisic
Copy link

dmilisic commented May 28, 2018

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 been config.model 'Product' do ... end.
The model name must be quoted.

@cristiansimioni
Copy link
Author

@dmilisic thank you very much! It saved me a lot of time. :)

But, any idea why it should be quoted?

@dmilisic
Copy link

dmilisic commented May 29, 2018

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 string config.model 'Product' do ... end works well with rails_admin.

It seems that ActiveStorage has its own initalizer which waits ActiveRecord to load before it inserts its macros. Perhaps rails_admin.rb initalizer is executed before active_storage.attached initalizer ?

@bperlik
Copy link

bperlik commented Aug 24, 2018

If you don't use require "rails/all', you'll need to require "active_storage" in your application.rb file

@tteurs
Copy link

tteurs commented Oct 1, 2018

Same issue here, is there any fix?

@tteurs
Copy link

tteurs commented Oct 3, 2018

declaring this on model solved the problem for me

`
class User < ApplicationRecord
has_one_attached :avatar

rails_admin do
  field :avatar, :active_storage
end

end
`

reference https://github.com/sferik/rails_admin/issues/2990#issuecomment-378220331

@iarobinson
Copy link

I'm having this problem too. Here's a strange thing:

if I comment out has_one_attached :avatar and start the server, it starts fine. Then I can uncomment has_one_attached :avatar, save the file and load the application, ActiveStorage functions.

But I don't want to have to comment out all active storage functions each time I start the server.

Rails version = 5.2.1
Ruby version = 2.4.4

Here's the model:

class User < ApplicationRecord
  has_person_name

  rolify
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable

  # For reasons I can't understand. This doesn't work here. I'm developing it in organizations for the time being.
  has_one_attached :avatar
  has_and_belongs_to_many :organizations
end

Here's the application.rb file:

require_relative 'boot'
require 'rails/all'

# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)

module *ProjectName*
  class Application < Rails::Application
    # Initialize configuration defaults for originally generated Rails version.
    config.load_defaults 5.2

    config.generators do |g|
      g.orm                 :active_record
      g.template_engine     :erb
      g.test_framework      false
      g.stylesheets         false
      g.javascripts         false
      g.helper              false
      g.scaffold_stylesheet false
    end
  end
end

Here's the Error Message:

$ rails s
/Users/silverSheep/.rvm/gems/ruby-2.4.4/gems/pry-rails-0.3.6/lib/pry-rails/prompt.rb:36: warning: constant Pry::Prompt::MAP is deprecated
=> Booting Puma
=> Rails 5.2.1 application starting in development 
=> Run `rails server -h` for more startup options
Exiting
/Users/silverSheep/.rvm/gems/ruby-2.4.4/gems/activerecord-5.2.1/lib/active_record/dynamic_matchers.rb:22:in `method_missing': undefined method `has_one_attached' for User (call 'User.connection' to establish a connection):Class (NoMethodError)
Did you mean?  has_person_name
	from /Users/silverSheep/Desktop/code/withBetter/projectName/app/models/user.rb:3:in `<class:User>'
	from /Users/silverSheep/Desktop/code/withBetter/projectName/app/models/user.rb:1:in `<main>'
	from /Users/silverSheep/.rvm/gems/ruby-2.4.4/gems/bootsnap-1.3.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:50:in `load'
	from /Users/silverSheep/.rvm/gems/ruby-2.4.4/gems/bootsnap-1.3.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:50:in `load'
	from /Users/silverSheep/.rvm/gems/ruby-2.4.4/gems/activesupport-5.2.1/lib/active_support/dependencies.rb:472:in `block in load_file'
	from /Users/silverSheep/.rvm/gems/ruby-2.4.4/gems/activesupport-5.2.1/lib/active_support/dependencies.rb:657:in `new_constants_in'
	from /Users/silverSheep/.rvm/gems/ruby-2.4.4/gems/activesupport-5.2.1/lib/active_support/dependencies.rb:471:in `load_file'
	from /Users/silverSheep/.rvm/gems/ruby-2.4.4/gems/activesupport-5.2.1/lib/active_support/dependencies.rb:369:in `block in require_or_load'
	from /Users/silverSheep/.rvm/gems/ruby-2.4.4/gems/activesupport-5.2.1/lib/active_support/dependencies.rb:37:in `block in load_interlock'
	from /Users/silverSheep/.rvm/gems/ruby-2.4.4/gems/activesupport-5.2.1/lib/active_support/dependencies/interlock.rb:14:in `block in loading'
	from /Users/silverSheep/.rvm/gems/ruby-2.4.4/gems/activesupport-5.2.1/lib/active_support/concurrency/share_lock.rb:151:in `exclusive'
	from /Users/silverSheep/.rvm/gems/ruby-2.4.4/gems/activesupport-5.2.1/lib/active_support/dependencies/interlock.rb:13:in `loading'
	from /Users/silverSheep/.rvm/gems/ruby-2.4.4/gems/activesupport-5.2.1/lib/active_support/dependencies.rb:37:in `load_interlock'
	from /Users/silverSheep/.rvm/gems/ruby-2.4.4/gems/activesupport-5.2.1/lib/active_support/dependencies.rb:352:in `require_or_load'
	from /Users/silverSheep/.rvm/gems/ruby-2.4.4/gems/bootsnap-1.3.2/lib/bootsnap/load_path_cache/core_ext/active_support.rb:46:in `block in require_or_load'
	from /Users/silverSheep/.rvm/gems/ruby-2.4.4/gems/bootsnap-1.3.2/lib/bootsnap/load_path_cache/core_ext/active_support.rb:16:in `allow_bootsnap_retry'
	from /Users/silverSheep/.rvm/gems/ruby-2.4.4/gems/bootsnap-1.3.2/lib/bootsnap/load_path_cache/core_ext/active_support.rb:45:in `require_or_load'
	from /Users/silverSheep/.rvm/gems/ruby-2.4.4/gems/activesupport-5.2.1/lib/active_support/dependencies.rb:506:in `load_missing_constant'
	from /Users/silverSheep/.rvm/gems/ruby-2.4.4/gems/bootsnap-1.3.2/lib/bootsnap/load_path_cache/core_ext/active_support.rb:58:in `block in load_missing_constant'
	from /Users/silverSheep/.rvm/gems/ruby-2.4.4/gems/bootsnap-1.3.2/lib/bootsnap/load_path_cache/core_ext/active_support.rb:16:in `allow_bootsnap_retry'
	from /Users/silverSheep/.rvm/gems/ruby-2.4.4/gems/bootsnap-1.3.2/lib/bootsnap/load_path_cache/core_ext/active_support.rb:57:in `load_missing_constant'
	from /Users/silverSheep/.rvm/gems/ruby-2.4.4/gems/activesupport-5.2.1/lib/active_support/dependencies.rb:193:in `const_missing'
	from /Users/silverSheep/.rvm/gems/ruby-2.4.4/gems/activesupport-

...(shortened for brevity)....

	from bin/rails:3:in `load'
	from bin/rails:3:in `<main>'

@tteurs - declaring it on the model like you did above didn't help.
@dmilisic - Where did you put config.model 'Product' do ... end to make it work? What was inside that block?

@dmilisic
Copy link

dmilisic commented Nov 21, 2018

Full stack trace might be helpful. The problem might be in initalizers. I had rails_admin.rb initializer which loaded my Product model. Models should not be loaded from initializers - it breaks ActiveStorage helpers.
@iarobinson take a look your initializers (rails_admin, devise, ...). If the User model is referenced as a constant, it triggers loading of your User model - and breaks ActiveStorage (models are usually loaded after all initializers).
I replaced the config.model Product with config.model 'Product' in the rails_admin.rb initalizer - that solved my issue.

@sideshowbandana
Copy link

Hey guys,

Not sure if this is related, but thought I might bring it up -- Shopify/bootsnap#218

@headius
Copy link

headius commented Mar 16, 2019

I just ran into this and the bug @sideshowbandana mentions, Shopify/bootsnap#218, was the cause.

Shopify/bootsnap#218 (comment)

@tteurs
Copy link

tteurs commented Mar 18, 2019

I solve my issue removing all configuration model from initializer( rails_admin.rb ) and putting each configuration in the respective model.

@burke
Copy link

burke commented Mar 20, 2019

Fixed by Shopify/bootsnap#249

@caiofilipemr
Copy link

caiofilipemr commented Sep 17, 2019

I'm getting this error too, but in my case is on Papertrail configuration:

RailsAdmin.config do |config|
  ...
  config.audit_with :paper_trail, 'User', 'PaperTrail::Version'
  ...
end

User model:

class User < ApplicationRecord
  ...
  has_one_attached :avatar
  ...
end

The error message:

`method_missing': undefined method `has_one_attached' for User (call 'User.connection' to establish a connection):Class (NoMethodError)
Full stacktrace
/usr/local/bundle/gems/activerecord-6.0.0/lib/active_record/dynamic_matchers.rb:22:in `method_missing': undefined method `has_one_attached' for User (call 'User.connection' to establish a connection):Class (NoMethodError)
        from /app/app/models/user.rb:150:in `<class:User>'
        from /app/app/models/user.rb:64:in `<main>'
        from /usr/local/bundle/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:54:in `load'
        from /usr/local/bundle/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:54:in `load'
        from /usr/local/bundle/gems/activesupport-6.0.0/lib/active_support/dependencies.rb:511:in `block in load_file'
        from /usr/local/bundle/gems/activesupport-6.0.0/lib/active_support/dependencies.rb:702:in `new_constants_in'
        from /usr/local/bundle/gems/activesupport-6.0.0/lib/active_support/dependencies.rb:510:in `load_file'
        from /usr/local/bundle/gems/activesupport-6.0.0/lib/active_support/dependencies.rb:407:in `block in require_or_load'
        from /usr/local/bundle/gems/activesupport-6.0.0/lib/active_support/dependencies.rb:40:in `block in load_interlock'
        from /usr/local/bundle/gems/activesupport-6.0.0/lib/active_support/dependencies/interlock.rb:14:in `block in loading'
        from /usr/local/bundle/gems/activesupport-6.0.0/lib/active_support/concurrency/share_lock.rb:151:in `exclusive'
        from /usr/local/bundle/gems/activesupport-6.0.0/lib/active_support/dependencies/interlock.rb:13:in `loading'
        from /usr/local/bundle/gems/activesupport-6.0.0/lib/active_support/dependencies.rb:40:in `load_interlock'
        from /usr/local/bundle/gems/activesupport-6.0.0/lib/active_support/dependencies.rb:390:in `require_or_load'
        from /usr/local/bundle/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/active_support.rb:48:in `block in require_or_load'
        from /usr/local/bundle/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/active_support.rb:16:in `allow_bootsnap_retry'
        from /usr/local/bundle/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/active_support.rb:47:in `require_or_load'
        from /usr/local/bundle/gems/activesupport-6.0.0/lib/active_support/dependencies.rb:545:in `load_missing_constant'
        from /usr/local/bundle/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/active_support.rb:60:in `block in load_missing_constant'
        from /usr/local/bundle/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/active_support.rb:16:in `allow_bootsnap_retry'
        from /usr/local/bundle/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/active_support.rb:59:in `load_missing_constant'
        from /usr/local/bundle/gems/activesupport-6.0.0/lib/active_support/dependencies.rb:214:in `const_missing'
        from /usr/local/bundle/gems/activesupport-6.0.0/lib/active_support/dependencies.rb:582:in `load_missing_constant'
        from /usr/local/bundle/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/active_support.rb:60:in `block in load_missing_constant'
        from /usr/local/bundle/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/active_support.rb:16:in `allow_bootsnap_retry'
        from /usr/local/bundle/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/active_support.rb:59:in `load_missing_constant'
        from /usr/local/bundle/gems/activesupport-6.0.0/lib/active_support/dependencies.rb:214:in `const_missing'
        from /app/app/models/subscription.rb:121:in `<class:Subscription>'
        from /app/app/models/subscription.rb:52:in `<main>'
        from /usr/local/bundle/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:54:in `load'
        from /usr/local/bundle/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:54:in `load'
        from /usr/local/bundle/gems/activesupport-6.0.0/lib/active_support/dependencies.rb:511:in `block in load_file'
        from /usr/local/bundle/gems/activesupport-6.0.0/lib/active_support/dependencies.rb:702:in `new_constants_in'
        from /usr/local/bundle/gems/activesupport-6.0.0/lib/active_support/dependencies.rb:510:in `load_file'
        from /usr/local/bundle/gems/activesupport-6.0.0/lib/active_support/dependencies.rb:407:in `block in require_or_load'
        from /usr/local/bundle/gems/activesupport-6.0.0/lib/active_support/dependencies.rb:40:in `block in load_interlock'
        from /usr/local/bundle/gems/activesupport-6.0.0/lib/active_support/dependencies/interlock.rb:14:in `block in loading'
        from /usr/local/bundle/gems/activesupport-6.0.0/lib/active_support/concurrency/share_lock.rb:151:in `exclusive'
        from /usr/local/bundle/gems/activesupport-6.0.0/lib/active_support/dependencies/interlock.rb:13:in `loading'
        from /usr/local/bundle/gems/activesupport-6.0.0/lib/active_support/dependencies.rb:40:in `load_interlock'
        from /usr/local/bundle/gems/activesupport-6.0.0/lib/active_support/dependencies.rb:390:in `require_or_load'
        from /usr/local/bundle/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/active_support.rb:48:in `block in require_or_load'
        from /usr/local/bundle/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/active_support.rb:16:in `allow_bootsnap_retry'
        from /usr/local/bundle/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/active_support.rb:47:in `require_or_load'
        from /usr/local/bundle/gems/activesupport-6.0.0/lib/active_support/dependencies.rb:545:in `load_missing_constant'
        from /usr/local/bundle/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/active_support.rb:60:in `block in load_missing_constant'
        from /usr/local/bundle/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/active_support.rb:16:in `allow_bootsnap_retry'
        from /usr/local/bundle/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/active_support.rb:59:in `load_missing_constant'
        from /usr/local/bundle/gems/activesupport-6.0.0/lib/active_support/dependencies.rb:214:in `const_missing'
        from /usr/local/bundle/gems/activesupport-6.0.0/lib/active_support/dependencies.rb:582:in `load_missing_constant'
        from /usr/local/bundle/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/active_support.rb:60:in `block in load_missing_constant'
        from /usr/local/bundle/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/active_support.rb:16:in `allow_bootsnap_retry'
        from /usr/local/bundle/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/active_support.rb:59:in `load_missing_constant'
        from /usr/local/bundle/gems/activesupport-6.0.0/lib/active_support/dependencies.rb:214:in `const_missing'
        from /app/app/helpers/subscriptions_helper.rb:46:in `<module:SubscriptionsHelper>'
        from /app/app/helpers/subscriptions_helper.rb:4:in `<main>'
        from /usr/local/bundle/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:54:in `load'
        from /usr/local/bundle/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:54:in `load'
        from /usr/local/bundle/gems/activesupport-6.0.0/lib/active_support/dependencies.rb:511:in `block in load_file'
        from /usr/local/bundle/gems/activesupport-6.0.0/lib/active_support/dependencies.rb:702:in `new_constants_in'
        from /usr/local/bundle/gems/activesupport-6.0.0/lib/active_support/dependencies.rb:510:in `load_file'
        from /usr/local/bundle/gems/activesupport-6.0.0/lib/active_support/dependencies.rb:407:in `block in require_or_load'
        from /usr/local/bundle/gems/activesupport-6.0.0/lib/active_support/dependencies.rb:40:in `block in load_interlock'
        from /usr/local/bundle/gems/activesupport-6.0.0/lib/active_support/dependencies/interlock.rb:14:in `block in loading'
        from /usr/local/bundle/gems/activesupport-6.0.0/lib/active_support/concurrency/share_lock.rb:151:in `exclusive'
        from /usr/local/bundle/gems/activesupport-6.0.0/lib/active_support/dependencies/interlock.rb:13:in `loading'
        from /usr/local/bundle/gems/activesupport-6.0.0/lib/active_support/dependencies.rb:40:in `load_interlock'
        from /usr/local/bundle/gems/activesupport-6.0.0/lib/active_support/dependencies.rb:390:in `require_or_load'
        from /usr/local/bundle/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/active_support.rb:48:in `block in require_or_load'
        from /usr/local/bundle/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/active_support.rb:16:in `allow_bootsnap_retry'
        from /usr/local/bundle/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/active_support.rb:47:in `require_or_load'
        from /usr/local/bundle/gems/activesupport-6.0.0/lib/active_support/dependencies.rb:368:in `depend_on'
        from /usr/local/bundle/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/active_support.rb:85:in `depend_on'
        from /usr/local/bundle/gems/activesupport-6.0.0/lib/active_support/dependencies.rb:280:in `require_dependency'
        from /usr/local/bundle/gems/actionpack-6.0.0/lib/abstract_controller/helpers.rb:149:in `block in modules_for_helpers'
        from /usr/local/bundle/gems/actionpack-6.0.0/lib/abstract_controller/helpers.rb:144:in `map!'
        from /usr/local/bundle/gems/actionpack-6.0.0/lib/abstract_controller/helpers.rb:144:in `modules_for_helpers'
        from /usr/local/bundle/gems/actionpack-6.0.0/lib/action_controller/metal/helpers.rb:94:in `modules_for_helpers'
        from /usr/local/bundle/gems/actionpack-6.0.0/lib/abstract_controller/helpers.rb:108:in `helper'
        from /usr/local/bundle/gems/actionpack-6.0.0/lib/action_controller/railties/helpers.rb:19:in `inherited'
        from /app/app/controllers/rails_admin_base_controller.rb:3:in `<main>'
        from /usr/local/bundle/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:54:in `load'
        from /usr/local/bundle/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:54:in `load'
        from /usr/local/bundle/gems/activesupport-6.0.0/lib/active_support/dependencies.rb:511:in `block in load_file'
        from /usr/local/bundle/gems/activesupport-6.0.0/lib/active_support/dependencies.rb:702:in `new_constants_in'
        from /usr/local/bundle/gems/activesupport-6.0.0/lib/active_support/dependencies.rb:510:in `load_file'
        from /usr/local/bundle/gems/activesupport-6.0.0/lib/active_support/dependencies.rb:407:in `block in require_or_load'
        from /usr/local/bundle/gems/activesupport-6.0.0/lib/active_support/dependencies.rb:40:in `block in load_interlock'
        from /usr/local/bundle/gems/activesupport-6.0.0/lib/active_support/dependencies/interlock.rb:14:in `block in loading'
        from /usr/local/bundle/gems/activesupport-6.0.0/lib/active_support/concurrency/share_lock.rb:151:in `exclusive'
        from /usr/local/bundle/gems/activesupport-6.0.0/lib/active_support/dependencies/interlock.rb:13:in `loading'
        from /usr/local/bundle/gems/activesupport-6.0.0/lib/active_support/dependencies.rb:40:in `load_interlock'
        from /usr/local/bundle/gems/activesupport-6.0.0/lib/active_support/dependencies.rb:390:in `require_or_load'
        from /usr/local/bundle/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/active_support.rb:48:in `block in require_or_load'
        from /usr/local/bundle/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/active_support.rb:16:in `allow_bootsnap_retry'
        from /usr/local/bundle/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/active_support.rb:47:in `require_or_load'
        from /usr/local/bundle/gems/activesupport-6.0.0/lib/active_support/dependencies.rb:545:in `load_missing_constant'
        from /usr/local/bundle/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/active_support.rb:60:in `block in load_missing_constant'
        from /usr/local/bundle/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/active_support.rb:16:in `allow_bootsnap_retry'
        from /usr/local/bundle/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/active_support.rb:59:in `load_missing_constant'
        from /usr/local/bundle/gems/activesupport-6.0.0/lib/active_support/dependencies.rb:214:in `const_missing'
        from /usr/local/bundle/gems/activesupport-6.0.0/lib/active_support/inflector/methods.rb:282:in `const_get'
        from /usr/local/bundle/gems/activesupport-6.0.0/lib/active_support/inflector/methods.rb:282:in `block in constantize'
        from /usr/local/bundle/gems/activesupport-6.0.0/lib/active_support/inflector/methods.rb:280:in `each'
        from /usr/local/bundle/gems/activesupport-6.0.0/lib/active_support/inflector/methods.rb:280:in `inject'
        from /usr/local/bundle/gems/activesupport-6.0.0/lib/active_support/inflector/methods.rb:280:in `constantize'
        from /usr/local/bundle/gems/activesupport-6.0.0/lib/active_support/core_ext/string/inflections.rb:68:in `constantize'
        from /usr/local/bundle/gems/rails_admin-2.0.0/app/controllers/rails_admin/application_controller.rb:13:in `<module:RailsAdmin>'
        from /usr/local/bundle/gems/rails_admin-2.0.0/app/controllers/rails_admin/application_controller.rb:3:in `<main>'
        from /usr/local/bundle/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:54:in `load'
        from /usr/local/bundle/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:54:in `load'
        from /usr/local/bundle/gems/activesupport-6.0.0/lib/active_support/dependencies.rb:511:in `block in load_file'
        from /usr/local/bundle/gems/activesupport-6.0.0/lib/active_support/dependencies.rb:702:in `new_constants_in'
        from /usr/local/bundle/gems/activesupport-6.0.0/lib/active_support/dependencies.rb:510:in `load_file'
        from /usr/local/bundle/gems/activesupport-6.0.0/lib/active_support/dependencies.rb:407:in `block in require_or_load'
        from /usr/local/bundle/gems/activesupport-6.0.0/lib/active_support/dependencies.rb:40:in `block in load_interlock'
        from /usr/local/bundle/gems/activesupport-6.0.0/lib/active_support/dependencies/interlock.rb:14:in `block in loading'
        from /usr/local/bundle/gems/activesupport-6.0.0/lib/active_support/concurrency/share_lock.rb:151:in `exclusive'
        from /usr/local/bundle/gems/activesupport-6.0.0/lib/active_support/dependencies/interlock.rb:13:in `loading'
        from /usr/local/bundle/gems/activesupport-6.0.0/lib/active_support/dependencies.rb:40:in `load_interlock'
        from /usr/local/bundle/gems/activesupport-6.0.0/lib/active_support/dependencies.rb:390:in `require_or_load'
        from /usr/local/bundle/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/active_support.rb:48:in `block in require_or_load'
        from /usr/local/bundle/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/active_support.rb:16:in `allow_bootsnap_retry'
        from /usr/local/bundle/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/active_support.rb:47:in `require_or_load'
        from /usr/local/bundle/gems/activesupport-6.0.0/lib/active_support/dependencies.rb:545:in `load_missing_constant'
        from /usr/local/bundle/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/active_support.rb:60:in `block in load_missing_constant'
        from /usr/local/bundle/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/active_support.rb:16:in `allow_bootsnap_retry'
        from /usr/local/bundle/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/active_support.rb:59:in `load_missing_constant'
        from /usr/local/bundle/gems/activesupport-6.0.0/lib/active_support/dependencies.rb:214:in `const_missing'
        from /usr/local/bundle/gems/rails_admin-2.0.0/lib/rails_admin/extensions/paper_trail/auditing_adapter.rb:55:in `setup'
        from /usr/local/bundle/gems/rails_admin-2.0.0/lib/rails_admin/config.rb:121:in `audit_with'
        from /app/config/initializers/rails_admin.rb:18:in `block in <main>'
        from /usr/local/bundle/gems/rails_admin-2.0.0/lib/rails_admin.rb:31:in `config'
        from /app/config/initializers/rails_admin.rb:2:in `<main>'
        from /usr/local/bundle/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:54:in `load'
        from /usr/local/bundle/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:54:in `load'
        from /usr/local/bundle/gems/activesupport-6.0.0/lib/active_support/dependencies.rb:319:in `block in load'
        from /usr/local/bundle/gems/activesupport-6.0.0/lib/active_support/dependencies.rb:291:in `load_dependency'
        from /usr/local/bundle/gems/activesupport-6.0.0/lib/active_support/dependencies.rb:319:in `load'
        from /usr/local/bundle/gems/railties-6.0.0/lib/rails/engine.rb:667:in `block in load_config_initializer'
        from /usr/local/bundle/gems/activesupport-6.0.0/lib/active_support/notifications.rb:182:in `instrument'
        from /usr/local/bundle/gems/railties-6.0.0/lib/rails/engine.rb:666:in `load_config_initializer'
        from /usr/local/bundle/gems/railties-6.0.0/lib/rails/engine.rb:624:in `block (2 levels) in <class:Engine>'
        from /usr/local/bundle/gems/railties-6.0.0/lib/rails/engine.rb:623:in `each'
        from /usr/local/bundle/gems/railties-6.0.0/lib/rails/engine.rb:623:in `block in <class:Engine>'
        from /usr/local/bundle/gems/railties-6.0.0/lib/rails/initializable.rb:32:in `instance_exec'
        from /usr/local/bundle/gems/railties-6.0.0/lib/rails/initializable.rb:32:in `run'
        from /usr/local/bundle/gems/railties-6.0.0/lib/rails/initializable.rb:61:in `block in run_initializers'
        from /usr/local/lib/ruby/2.6.0/tsort.rb:228:in `block in tsort_each'
        from /usr/local/lib/ruby/2.6.0/tsort.rb:350:in `block (2 levels) in each_strongly_connected_component'
        from /usr/local/lib/ruby/2.6.0/tsort.rb:422:in `block (2 levels) in each_strongly_connected_component_from'
        from /usr/local/lib/ruby/2.6.0/tsort.rb:431:in `each_strongly_connected_component_from'
        from /usr/local/lib/ruby/2.6.0/tsort.rb:421:in `block in each_strongly_connected_component_from'
        from /usr/local/bundle/gems/railties-6.0.0/lib/rails/initializable.rb:50:in `each'
        from /usr/local/bundle/gems/railties-6.0.0/lib/rails/initializable.rb:50:in `tsort_each_child'
        from /usr/local/lib/ruby/2.6.0/tsort.rb:415:in `call'
        from /usr/local/lib/ruby/2.6.0/tsort.rb:415:in `each_strongly_connected_component_from'
        from /usr/local/lib/ruby/2.6.0/tsort.rb:349:in `block in each_strongly_connected_component'
        from /usr/local/lib/ruby/2.6.0/tsort.rb:347:in `each'
        from /usr/local/lib/ruby/2.6.0/tsort.rb:347:in `call'
        from /usr/local/lib/ruby/2.6.0/tsort.rb:347:in `each_strongly_connected_component'
        from /usr/local/lib/ruby/2.6.0/tsort.rb:226:in `tsort_each'
        from /usr/local/lib/ruby/2.6.0/tsort.rb:205:in `tsort_each'
        from /usr/local/bundle/gems/railties-6.0.0/lib/rails/initializable.rb:60:in `run_initializers'
        from /usr/local/bundle/gems/railties-6.0.0/lib/rails/application.rb:363:in `initialize!'
        from /app/config/environment.rb:6:in `<main>'
        from /usr/local/bundle/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require'
        from /usr/local/bundle/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `block in require_with_bootsnap_lfi'
        from /usr/local/bundle/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/loaded_features_index.rb:92:in `register'
        from /usr/local/bundle/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `require_with_bootsnap_lfi'
        from /usr/local/bundle/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require'
        from /usr/local/bundle/gems/activesupport-6.0.0/lib/active_support/dependencies.rb:325:in `block in require'
        from /usr/local/bundle/gems/activesupport-6.0.0/lib/active_support/dependencies.rb:291:in `load_dependency'
        from /usr/local/bundle/gems/activesupport-6.0.0/lib/active_support/dependencies.rb:325:in `require'
        from /usr/local/bundle/gems/spring-2.1.0/lib/spring/application.rb:106:in `preload'
        from /usr/local/bundle/gems/spring-2.1.0/lib/spring/application.rb:157:in `serve'
        from /usr/local/bundle/gems/spring-2.1.0/lib/spring/application.rb:145:in `block in run'
        from /usr/local/bundle/gems/spring-2.1.0/lib/spring/application.rb:139:in `loop'
        from /usr/local/bundle/gems/spring-2.1.0/lib/spring/application.rb:139:in `run'
        from /usr/local/bundle/gems/spring-2.1.0/lib/spring/application/boot.rb:19:in `<top (required)>'
        from /usr/local/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
        from /usr/local/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
        from -e:1:in `<main>'

Any ideas in how to solve this?

@dmilisic
Copy link

Can you post the code of your rails_admin initializer? Or perhaps line 18?

@caiofilipemr
Copy link

Line 18 it is the papertrail configuration:

  config.audit_with :paper_trail, 'User', 'PaperTrail::Version'

@caiofilipemr
Copy link

@dmilisic thank you for the support! We have managed to solve the problem. The Subscription model was calling a constant defined in the User model and then make the User be load before has_one_attached creation (I think!?). From the previously stacktrace:

...
from /usr/local/bundle/gems/activesupport-6.0.0/lib/active_support/dependencies.rb:214:in `const_missing'
from /app/app/models/subscription.rb:121:in `<class:Subscription>'
...

So we just copy the constant to the Subscription class and then it worked.

@dmilisic
Copy link

Great to hear you managed to solve this issue. But the question remains: why was even Subscription model loaded (perhaps through SubscriptionsHelper)? Are they loaded from the rails_admin initializer?

@caiofilipemr
Copy link

caiofilipemr commented Sep 19, 2019

perhaps through SubscriptionsHelper

Yes, it use a constant from Subscription.

Are they loaded from the rails_admin initializer?

Not directly at least. Maybe all helpers are load before models!?

@serg2801
Copy link

@dmilisic #3025 (comment)
Thanks! Works for me. You did my day.

@kanyonbollinger
Copy link

I ignored this thread because I thought it would not solve my issue. Turns out I'm completely wrong. Thank you so much! @dmilisic

@mattwr18
Copy link

thanks a lot @dmilisic, your solution to use a string instead of a constant helped me a lot!!

@ghost
Copy link

ghost commented Aug 25, 2020

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_attached or has_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 method has_one_attached will be missing.

I added this code, taken from active_storage/engine, at the end of config/application.rb, and it worked for me.

# config/application.rb
require "active_storage/attached"
ActiveSupport.on_load(:active_record) do
  extend ActiveStorage::Attached::Macros
end

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

No branches or pull requests