rails: ActiveStorage 'has_one_attached', 'has_many_attached' NoMethodError

Steps to reproduce

  1. Updated rails gem from 5.1.4 to 5.2.0:
rails (5.2.0)
      actioncable (= 5.2.0)
      actionmailer (= 5.2.0)
      actionpack (= 5.2.0)
      actionview (= 5.2.0)
      activejob (= 5.2.0)
      activemodel (= 5.2.0)
      activerecord (= 5.2.0)
      activestorage (= 5.2.0)
      activesupport (= 5.2.0)
      bundler (>= 1.3.0)
      railties (= 5.2.0)
      sprockets-rails (>= 2.0.0)
  1. ran rails active_storage:install
  2. ran rails db:migrate: 2 new tables created, active_storage_attachments and active_storage_blobs
  create_table "active_storage_attachments", force: :cascade do |t|
    t.string "name", null: false
    t.string "record_type", null: false
    t.bigint "record_id", null: false
    t.bigint "blob_id", null: false
    t.datetime "created_at", null: false
    t.index ["blob_id"], name: "index_active_storage_attachments_on_blob_id"
    t.index ["record_type", "record_id", "name", "blob_id"], name: "index_active_storage_attachments_uniqueness", unique: true
  end

  create_table "active_storage_blobs", force: :cascade do |t|
    t.string "key", null: false
    t.string "filename", null: false
    t.string "content_type"
    t.text "metadata"
    t.bigint "byte_size", null: false
    t.string "checksum", null: false
    t.datetime "created_at", null: false
    t.index ["key"], name: "index_active_storage_blobs_on_key", unique: true
  end
  1. Created config/storage.yml file:
local:
  service: Disk
  root: <%= Rails.root.join("storage") %>
 
local_test:
  service: Disk
  root: <%= Rails.root.join("tmp/storage") %>

Added config.active_storage.service to config/environments/development.rb

Rails.application.configure do
  # Tell Active Storage which service to use by setting Rails.application.config.active_storage.service. 
  # Because each environment will likely use a different service, it is recommended to do this on a per-environment basis.
  config.active_storage.service = :local
end
  1. Added ‘has_many_attached :documents’ to model.rb file:
class Model < ActiveRecord::Base
  has_many_attached  :documents
  1. Tried ‘has_one_attached :avatar’ to model.rb file:
class Model < ActiveRecord::Base
  has_one_attached  :avatar

Expected behavior

The server should start (with rails s) or the console should start (with rails c)

Actual behavior

has_many_attached: /Users/username/.rvm/rubies/ruby-2.5.1/lib/ruby/gems/2.5.0/gems/activerecord-5.2.0/lib/active_record/dynamic_matchers.rb:22:in method_missing': undefined method has_many_attached’ for Model (call ‘Model.connection’ to establish a connection):Class (NoMethodError)

has_one_attached: /Users/username/.rvm/rubies/ruby-2.5.1/lib/ruby/gems/2.5.0/gems/activerecord-5.2.0/lib/active_record/dynamic_matchers.rb:22:in method_missing': undefined method has_one_attached’ for Model (call ‘Model.connection’ to establish a connection):Class (NoMethodError)

for both ‘rails s’ and ‘rails c’ commands mentioned above has_one_attached :avatar gives the same erorr

System configuration

Rails version: 5.2.0 Ruby version: 2.5.1

Attempts to resolve the issue

Reinstalling all gems including ruby and rails

Full trace

Traceback (most recent call last):
	84: from bin/rails:3:in `<main>'
	83: from bin/rails:3:in `load'
	82: from /Users/username/Documents/repository-name/bin/spring:15:in `<top (required)>'
	81: from /Users/username/Documents/repository-name/bin/spring:15:in `require'
	80: from /Users/username/.rvm/gems/ruby-2.5.1/gems/spring-2.0.2/lib/spring/binstub.rb:31:in `<top (required)>'
	79: from /Users/username/.rvm/gems/ruby-2.5.1/gems/spring-2.0.2/lib/spring/binstub.rb:31:in `load'
	78: from /Users/username/.rvm/gems/ruby-2.5.1/gems/spring-2.0.2/bin/spring:49:in `<top (required)>'
	77: from /Users/username/.rvm/gems/ruby-2.5.1/gems/spring-2.0.2/lib/spring/client.rb:30:in `run'
	76: from /Users/username/.rvm/gems/ruby-2.5.1/gems/spring-2.0.2/lib/spring/client/command.rb:7:in `call'
	75: from /Users/username/.rvm/gems/ruby-2.5.1/gems/spring-2.0.2/lib/spring/client/rails.rb:28:in `call'
	74: from /Users/username/.rvm/gems/ruby-2.5.1/gems/spring-2.0.2/lib/spring/client/rails.rb:28:in `load'
	73: from /Users/username/Documents/repository-name/bin/rails:9:in `<top (required)>'
	72: from /Users/username/Documents/repository-name/bin/rails:9:in `require'
	71: from /Users/username/.rvm/rubies/ruby-2.5.1/lib/ruby/gems/2.5.0/gems/railties-5.2.0/lib/rails/commands.rb:18:in `<top (required)>'
	70: from /Users/username/.rvm/rubies/ruby-2.5.1/lib/ruby/gems/2.5.0/gems/railties-5.2.0/lib/rails/command.rb:46:in `invoke'
	69: from /Users/username/.rvm/rubies/ruby-2.5.1/lib/ruby/gems/2.5.0/gems/railties-5.2.0/lib/rails/command/base.rb:65:in `perform'
	68: from /Users/username/.rvm/rubies/ruby-2.5.1/lib/ruby/gems/2.5.0/gems/thor-0.20.0/lib/thor.rb:387:in `dispatch'
	67: from /Users/username/.rvm/rubies/ruby-2.5.1/lib/ruby/gems/2.5.0/gems/thor-0.20.0/lib/thor/invocation.rb:126:in `invoke_command'
	66: from /Users/username/.rvm/rubies/ruby-2.5.1/lib/ruby/gems/2.5.0/gems/thor-0.20.0/lib/thor/command.rb:27:in `run'
	65: from /Users/username/.rvm/rubies/ruby-2.5.1/lib/ruby/gems/2.5.0/gems/railties-5.2.0/lib/rails/commands/server/server_command.rb:142:in `perform'
	64: from /Users/username/.rvm/rubies/ruby-2.5.1/lib/ruby/gems/2.5.0/gems/railties-5.2.0/lib/rails/commands/server/server_command.rb:142:in `tap'
	63: from /Users/username/.rvm/rubies/ruby-2.5.1/lib/ruby/gems/2.5.0/gems/railties-5.2.0/lib/rails/commands/server/server_command.rb:147:in `block in perform'
	62: from /Users/username/.rvm/rubies/ruby-2.5.1/lib/ruby/gems/2.5.0/gems/railties-5.2.0/lib/rails/commands/server/server_command.rb:51:in `start'
	61: from /Users/username/.rvm/rubies/ruby-2.5.1/lib/ruby/gems/2.5.0/gems/railties-5.2.0/lib/rails/commands/server/server_command.rb:89:in `log_to_stdout'
	60: from /Users/username/.rvm/rubies/ruby-2.5.1/lib/ruby/gems/2.5.0/gems/rack-2.0.5/lib/rack/server.rb:354:in `wrapped_app'
	59: from /Users/username/.rvm/rubies/ruby-2.5.1/lib/ruby/gems/2.5.0/gems/railties-5.2.0/lib/rails/commands/server/server_command.rb:27:in `app'
	58: from /Users/username/.rvm/rubies/ruby-2.5.1/lib/ruby/gems/2.5.0/gems/rack-2.0.5/lib/rack/server.rb:219:in `app'
	57: from /Users/username/.rvm/rubies/ruby-2.5.1/lib/ruby/gems/2.5.0/gems/rack-2.0.5/lib/rack/server.rb:319:in `build_app_and_options_from_config'
	56: from /Users/username/.rvm/rubies/ruby-2.5.1/lib/ruby/gems/2.5.0/gems/rack-2.0.5/lib/rack/builder.rb:40:in `parse_file'
	55: from /Users/username/.rvm/rubies/ruby-2.5.1/lib/ruby/gems/2.5.0/gems/rack-2.0.5/lib/rack/builder.rb:49:in `new_from_string'
	54: from /Users/username/.rvm/rubies/ruby-2.5.1/lib/ruby/gems/2.5.0/gems/rack-2.0.5/lib/rack/builder.rb:49:in `eval'
	53: from config.ru:in `<main>'
	52: from config.ru:in `new'
	51: from /Users/username/.rvm/rubies/ruby-2.5.1/lib/ruby/gems/2.5.0/gems/rack-2.0.5/lib/rack/builder.rb:55:in `initialize'
	50: from /Users/username/.rvm/rubies/ruby-2.5.1/lib/ruby/gems/2.5.0/gems/rack-2.0.5/lib/rack/builder.rb:55:in `instance_eval'
	49: from config.ru:3:in `block in <main>'
	48: from config.ru:3:in `require_relative'
	47: from /Users/username/Documents/repository-name/config/environment.rb:5:in `<top (required)>'
	46: from /Users/username/.rvm/rubies/ruby-2.5.1/lib/ruby/gems/2.5.0/gems/railties-5.2.0/lib/rails/application.rb:361:in `initialize!'
	45: from /Users/username/.rvm/rubies/ruby-2.5.1/lib/ruby/gems/2.5.0/gems/railties-5.2.0/lib/rails/initializable.rb:60:in `run_initializers'
	44: from /Users/username/.rvm/rubies/ruby-2.5.1/lib/ruby/2.5.0/tsort.rb:205:in `tsort_each'
	43: from /Users/username/.rvm/rubies/ruby-2.5.1/lib/ruby/2.5.0/tsort.rb:226:in `tsort_each'
	42: from /Users/username/.rvm/rubies/ruby-2.5.1/lib/ruby/2.5.0/tsort.rb:347:in `each_strongly_connected_component'
	41: from /Users/username/.rvm/rubies/ruby-2.5.1/lib/ruby/2.5.0/tsort.rb:347:in `call'
	40: from /Users/username/.rvm/rubies/ruby-2.5.1/lib/ruby/2.5.0/tsort.rb:347:in `each'
	39: from /Users/username/.rvm/rubies/ruby-2.5.1/lib/ruby/2.5.0/tsort.rb:349:in `block in each_strongly_connected_component'
	38: from /Users/username/.rvm/rubies/ruby-2.5.1/lib/ruby/2.5.0/tsort.rb:415:in `each_strongly_connected_component_from'
	37: from /Users/username/.rvm/rubies/ruby-2.5.1/lib/ruby/2.5.0/tsort.rb:415:in `call'
	36: from /Users/username/.rvm/rubies/ruby-2.5.1/lib/ruby/gems/2.5.0/gems/railties-5.2.0/lib/rails/initializable.rb:50:in `tsort_each_child'
	35: from /Users/username/.rvm/rubies/ruby-2.5.1/lib/ruby/gems/2.5.0/gems/railties-5.2.0/lib/rails/initializable.rb:50:in `each'
	34: from /Users/username/.rvm/rubies/ruby-2.5.1/lib/ruby/2.5.0/tsort.rb:421:in `block in each_strongly_connected_component_from'
	33: from /Users/username/.rvm/rubies/ruby-2.5.1/lib/ruby/2.5.0/tsort.rb:431:in `each_strongly_connected_component_from'
	32: from /Users/username/.rvm/rubies/ruby-2.5.1/lib/ruby/2.5.0/tsort.rb:422:in `block (2 levels) in each_strongly_connected_component_from'
	31: from /Users/username/.rvm/rubies/ruby-2.5.1/lib/ruby/2.5.0/tsort.rb:350:in `block (2 levels) in each_strongly_connected_component'
	30: from /Users/username/.rvm/rubies/ruby-2.5.1/lib/ruby/2.5.0/tsort.rb:228:in `block in tsort_each'
	29: from /Users/username/.rvm/rubies/ruby-2.5.1/lib/ruby/gems/2.5.0/gems/railties-5.2.0/lib/rails/initializable.rb:61:in `block in run_initializers'
	28: from /Users/username/.rvm/rubies/ruby-2.5.1/lib/ruby/gems/2.5.0/gems/railties-5.2.0/lib/rails/initializable.rb:32:in `run'
	27: from /Users/username/.rvm/rubies/ruby-2.5.1/lib/ruby/gems/2.5.0/gems/railties-5.2.0/lib/rails/initializable.rb:32:in `instance_exec'
	26: from /Users/username/.rvm/rubies/ruby-2.5.1/lib/ruby/gems/2.5.0/gems/railties-5.2.0/lib/rails/engine.rb:613:in `block in <class:Engine>'
	25: from /Users/username/.rvm/rubies/ruby-2.5.1/lib/ruby/gems/2.5.0/gems/railties-5.2.0/lib/rails/engine.rb:613:in `each'
	24: from /Users/username/.rvm/rubies/ruby-2.5.1/lib/ruby/gems/2.5.0/gems/railties-5.2.0/lib/rails/engine.rb:614:in `block (2 levels) in <class:Engine>'
	23: from /Users/username/.rvm/rubies/ruby-2.5.1/lib/ruby/gems/2.5.0/gems/railties-5.2.0/lib/rails/engine.rb:656:in `load_config_initializer'
	22: from /Users/username/.rvm/rubies/ruby-2.5.1/lib/ruby/gems/2.5.0/gems/activesupport-5.2.0/lib/active_support/notifications.rb:170:in `instrument'
	21: from /Users/username/.rvm/rubies/ruby-2.5.1/lib/ruby/gems/2.5.0/gems/railties-5.2.0/lib/rails/engine.rb:657:in `block in load_config_initializer'
	20: from /Users/username/.rvm/rubies/ruby-2.5.1/lib/ruby/gems/2.5.0/gems/activesupport-5.2.0/lib/active_support/dependencies.rb:277:in `load'
	19: from /Users/username/.rvm/rubies/ruby-2.5.1/lib/ruby/gems/2.5.0/gems/activesupport-5.2.0/lib/active_support/dependencies.rb:249:in `load_dependency'
	18: from /Users/username/.rvm/rubies/ruby-2.5.1/lib/ruby/gems/2.5.0/gems/activesupport-5.2.0/lib/active_support/dependencies.rb:277:in `block in load'
	17: from /Users/username/.rvm/rubies/ruby-2.5.1/lib/ruby/gems/2.5.0/gems/activesupport-5.2.0/lib/active_support/dependencies.rb:277:in `load'
	16: from /Users/username/Documents/repository-name/config/initializers/constants.rb:4:in `<top (required)>'
	15: from /Users/username/.rvm/rubies/ruby-2.5.1/lib/ruby/gems/2.5.0/gems/activesupport-5.2.0/lib/active_support/dependencies.rb:193:in `const_missing'
	14: from /Users/username/.rvm/rubies/ruby-2.5.1/lib/ruby/gems/2.5.0/gems/activesupport-5.2.0/lib/active_support/dependencies.rb:502:in `load_missing_constant'
	13: from /Users/username/.rvm/rubies/ruby-2.5.1/lib/ruby/gems/2.5.0/gems/activesupport-5.2.0/lib/active_support/dependencies.rb:348:in `require_or_load'
	12: from /Users/username/.rvm/rubies/ruby-2.5.1/lib/ruby/gems/2.5.0/gems/activesupport-5.2.0/lib/active_support/dependencies.rb:37:in `load_interlock'
	11: from /Users/username/.rvm/rubies/ruby-2.5.1/lib/ruby/gems/2.5.0/gems/activesupport-5.2.0/lib/active_support/dependencies/interlock.rb:13:in `loading'
	10: from /Users/username/.rvm/rubies/ruby-2.5.1/lib/ruby/gems/2.5.0/gems/activesupport-5.2.0/lib/active_support/concurrency/share_lock.rb:151:in `exclusive'
	 9: from /Users/username/.rvm/rubies/ruby-2.5.1/lib/ruby/gems/2.5.0/gems/activesupport-5.2.0/lib/active_support/dependencies/interlock.rb:14:in `block in loading'
	 8: from /Users/username/.rvm/rubies/ruby-2.5.1/lib/ruby/gems/2.5.0/gems/activesupport-5.2.0/lib/active_support/dependencies.rb:37:in `block in load_interlock'
	 7: from /Users/username/.rvm/rubies/ruby-2.5.1/lib/ruby/gems/2.5.0/gems/activesupport-5.2.0/lib/active_support/dependencies.rb:365:in `block in require_or_load'
	 6: from /Users/username/.rvm/rubies/ruby-2.5.1/lib/ruby/gems/2.5.0/gems/activesupport-5.2.0/lib/active_support/dependencies.rb:467:in `load_file'
	 5: from /Users/username/.rvm/rubies/ruby-2.5.1/lib/ruby/gems/2.5.0/gems/activesupport-5.2.0/lib/active_support/dependencies.rb:653:in `new_constants_in'
	 4: from /Users/username/.rvm/rubies/ruby-2.5.1/lib/ruby/gems/2.5.0/gems/activesupport-5.2.0/lib/active_support/dependencies.rb:468:in `block in load_file'
	 3: from /Users/username/.rvm/rubies/ruby-2.5.1/lib/ruby/gems/2.5.0/gems/activesupport-5.2.0/lib/active_support/dependencies.rb:468:in `load'
	 2: from /Users/username/Documents/repository-name/app/models/model.rb:1:in `<top (required)>'
	 1: from /Users/username/Documents/repository-name/app/models/model.rb:2:in `<class:Model>'
/Users/username/.rvm/rubies/ruby-2.5.1/lib/ruby/gems/2.5.0/gems/activerecord-5.2.0/lib/active_record/dynamic_matchers.rb:22:in `method_missing': undefined method `has_one_attached' for Model (call 'Model.connection' to establish a connection):Class (NoMethodError)

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 4
  • Comments: 17 (7 by maintainers)

Commits related to this issue

Most upvoted comments

I was seeing this issue as well. By looking at the trace I found I was referencing the model with the has_one_attached call in an initializer. When the constant autoloader kicked in and tried to load that class the ActiveStorage macros were not available causing the NoMethodError to bubble up. I was able to patch it in my case by removing the direct reference to the constant in my initializer. For some reason it looks like ActiveStorage is not fully set up when the initializers are run.

I’m not sure my issue is the exact issue happening here, but hopefully it will help in digging to the bottom of it.

There are two problem on loading Active Record models in initializers, one of them is that it will mess with the code reload, those models may not be able to be reloaded anymore.

Another one is that it will make active record load early, and that will not only causes problems like this one in this issue, but also other problems with many other gems and even with active record configurations.

That being said, the correct fix is to not load models in initializers. The application is not initialized yet, so there is no guarantee it will work.