crodev
crodev

Reputation: 1491

Undefined method `configuration_hash' for nil:NilClass when running ActiveAdmin/Devise generator inside of Rails Engine

so I am creating a separate engine inside of my app for admin functionality. I created the engine and added Devise and ActiveAdmin as dependencies inside of .gemspec.

  spec.add_dependency "rails", ">= 7.0.4.3"
  spec.add_dependency "devise"
  spec.add_dependency "activeadmin"

And then when I run engine_name/bin/rails g active_admin:install I get the following error undefined method 'configuration_hash' for nil:NilClass (NoMethodError).

ActiveRecord::Base.configurations.configs_for(env_name: Rails.env, name: "primary").configuration_hash

I believe it's happening because it can't find the database configuration even though it's defined in the main app, and I can access data from main app's models.

I tried copy/pasting database.yml from main app's config to engines config folder but it doesn't work. Am I missing something?

Here's a full stack trace:

/home/luka/.asdf/installs/ruby/3.2.1/lib/ruby/gems/3.2.0/gems/devise-4.9.0/lib/generators/active_record/devise_generator.rb:100:in `ar_config': undefined method `configuration_hash' for nil:NilClass (NoMethodError)

            ActiveRecord::Base.configurations.configs_for(env_name: Rails.env, name: "primary").configuration_hash
                                                                                               ^^^^^^^^^^^^^^^^^^^
    from /home/luka/.asdf/installs/ruby/3.2.1/lib/ruby/gems/3.2.0/gems/devise-4.9.0/lib/generators/active_record/devise_generator.rb:94:in `postgresql?'
    from /home/luka/.asdf/installs/ruby/3.2.1/lib/ruby/gems/3.2.0/gems/devise-4.9.0/lib/generators/active_record/devise_generator.rb:82:in `inet?'
    from /home/luka/.asdf/installs/ruby/3.2.1/lib/ruby/gems/3.2.0/gems/devise-4.9.0/lib/generators/active_record/devise_generator.rb:78:in `ip_column'
    from /home/luka/.asdf/installs/ruby/3.2.1/lib/ruby/gems/3.2.0/gems/devise-4.9.0/lib/generators/active_record/devise_generator.rb:60:in `migration_data'
    from (erb):6:in `migration_template'
    from /home/luka/.asdf/installs/ruby/3.2.1/lib/ruby/3.2.0/erb.rb:429:in `eval'
    from /home/luka/.asdf/installs/ruby/3.2.1/lib/ruby/3.2.0/erb.rb:429:in `result'
    from /home/luka/.asdf/installs/ruby/3.2.1/lib/ruby/gems/3.2.0/gems/railties-7.0.4.3/lib/rails/generators/migration.rb:66:in `block in migration_template'
    from /home/luka/.asdf/installs/ruby/3.2.1/lib/ruby/gems/3.2.0/gems/thor-1.2.1/lib/thor/actions/create_file.rb:53:in `render'
    from /home/luka/.asdf/installs/ruby/3.2.1/lib/ruby/gems/3.2.0/gems/railties-7.0.4.3/lib/rails/generators/actions/create_migration.rb:19:in `identical?'
    from /home/luka/.asdf/installs/ruby/3.2.1/lib/ruby/gems/3.2.0/gems/railties-7.0.4.3/lib/rails/generators/actions/create_migration.rb:50:in `on_conflict_behavior'
    from /home/luka/.asdf/installs/ruby/3.2.1/lib/ruby/gems/3.2.0/gems/thor-1.2.1/lib/thor/actions/empty_directory.rb:115:in `invoke_with_conflict_check'
    from /home/luka/.asdf/installs/ruby/3.2.1/lib/ruby/gems/3.2.0/gems/thor-1.2.1/lib/thor/actions/create_file.rb:60:in `invoke!'
    from /home/luka/.asdf/installs/ruby/3.2.1/lib/ruby/gems/3.2.0/gems/railties-7.0.4.3/lib/rails/generators/actions/create_migration.rb:25:in `invoke!'
    from /home/luka/.asdf/installs/ruby/3.2.1/lib/ruby/gems/3.2.0/gems/thor-1.2.1/lib/thor/actions.rb:93:in `action'
    from /home/luka/.asdf/installs/ruby/3.2.1/lib/ruby/gems/3.2.0/gems/railties-7.0.4.3/lib/rails/generators/migration.rb:36:in `create_migration'
    from /home/luka/.asdf/installs/ruby/3.2.1/lib/ruby/gems/3.2.0/gems/railties-7.0.4.3/lib/rails/generators/migration.rb:65:in `migration_template'
    from /home/luka/.asdf/installs/ruby/3.2.1/lib/ruby/gems/3.2.0/gems/devise-4.9.0/lib/generators/active_record/devise_generator.rb:20:in `copy_devise_migration'
    from /home/luka/.asdf/installs/ruby/3.2.1/lib/ruby/gems/3.2.0/gems/thor-1.2.1/lib/thor/command.rb:27:in `run'
    from /home/luka/.asdf/installs/ruby/3.2.1/lib/ruby/gems/3.2.0/gems/thor-1.2.1/lib/thor/invocation.rb:127:in `invoke_command'
    from /home/luka/.asdf/installs/ruby/3.2.1/lib/ruby/gems/3.2.0/gems/thor-1.2.1/lib/thor/invocation.rb:134:in `invoke_all'
    from /home/luka/.asdf/installs/ruby/3.2.1/lib/ruby/gems/3.2.0/gems/thor-1.2.1/lib/thor/group.rb:232:in `dispatch'
    from /home/luka/.asdf/installs/ruby/3.2.1/lib/ruby/gems/3.2.0/gems/thor-1.2.1/lib/thor/invocation.rb:116:in `invoke'
    from /home/luka/.asdf/installs/ruby/3.2.1/lib/ruby/gems/3.2.0/gems/activeadmin-3.0.0/lib/generators/active_admin/devise/devise_generator.rb:43:in `create_admin_user'
    from /home/luka/.asdf/installs/ruby/3.2.1/lib/ruby/gems/3.2.0/gems/thor-1.2.1/lib/thor/command.rb:27:in `run'
    from /home/luka/.asdf/installs/ruby/3.2.1/lib/ruby/gems/3.2.0/gems/thor-1.2.1/lib/thor/invocation.rb:127:in `invoke_command'
    from /home/luka/.asdf/installs/ruby/3.2.1/lib/ruby/gems/3.2.0/gems/thor-1.2.1/lib/thor/invocation.rb:134:in `block in invoke_all'
    from /home/luka/.asdf/installs/ruby/3.2.1/lib/ruby/gems/3.2.0/gems/thor-1.2.1/lib/thor/invocation.rb:134:in `each'
    from /home/luka/.asdf/installs/ruby/3.2.1/lib/ruby/gems/3.2.0/gems/thor-1.2.1/lib/thor/invocation.rb:134:in `map'
    from /home/luka/.asdf/installs/ruby/3.2.1/lib/ruby/gems/3.2.0/gems/thor-1.2.1/lib/thor/invocation.rb:134:in `invoke_all'
    from /home/luka/.asdf/installs/ruby/3.2.1/lib/ruby/gems/3.2.0/gems/thor-1.2.1/lib/thor/group.rb:232:in `dispatch'
    from /home/luka/.asdf/installs/ruby/3.2.1/lib/ruby/gems/3.2.0/gems/thor-1.2.1/lib/thor/invocation.rb:116:in `invoke'
    from /home/luka/.asdf/installs/ruby/3.2.1/lib/ruby/gems/3.2.0/gems/thor-1.2.1/lib/thor/group.rb:277:in `block in _invoke_for_class_method'
    from /home/luka/.asdf/installs/ruby/3.2.1/lib/ruby/gems/3.2.0/gems/thor-1.2.1/lib/thor/shell.rb:68:in `with_padding'
    from /home/luka/.asdf/installs/ruby/3.2.1/lib/ruby/gems/3.2.0/gems/thor-1.2.1/lib/thor/group.rb:266:in `_invoke_for_class_method'
    from /home/luka/.asdf/installs/ruby/3.2.1/lib/ruby/gems/3.2.0/gems/thor-1.2.1/lib/thor/group.rb:134:in `_invoke_from_option_users'
    from /home/luka/.asdf/installs/ruby/3.2.1/lib/ruby/gems/3.2.0/gems/thor-1.2.1/lib/thor/command.rb:27:in `run'
    from /home/luka/.asdf/installs/ruby/3.2.1/lib/ruby/gems/3.2.0/gems/thor-1.2.1/lib/thor/invocation.rb:127:in `invoke_command'
    from /home/luka/.asdf/installs/ruby/3.2.1/lib/ruby/gems/3.2.0/gems/thor-1.2.1/lib/thor/invocation.rb:134:in `block in invoke_all'
    from /home/luka/.asdf/installs/ruby/3.2.1/lib/ruby/gems/3.2.0/gems/thor-1.2.1/lib/thor/invocation.rb:134:in `each'
    from /home/luka/.asdf/installs/ruby/3.2.1/lib/ruby/gems/3.2.0/gems/thor-1.2.1/lib/thor/invocation.rb:134:in `map'
    from /home/luka/.asdf/installs/ruby/3.2.1/lib/ruby/gems/3.2.0/gems/thor-1.2.1/lib/thor/invocation.rb:134:in `invoke_all'
    from /home/luka/.asdf/installs/ruby/3.2.1/lib/ruby/gems/3.2.0/gems/thor-1.2.1/lib/thor/group.rb:232:in `dispatch'
    from /home/luka/.asdf/installs/ruby/3.2.1/lib/ruby/gems/3.2.0/gems/thor-1.2.1/lib/thor/base.rb:485:in `start'
    from /home/luka/.asdf/installs/ruby/3.2.1/lib/ruby/gems/3.2.0/gems/railties-7.0.4.3/lib/rails/generators.rb:263:in `invoke'
    from /home/luka/.asdf/installs/ruby/3.2.1/lib/ruby/gems/3.2.0/gems/railties-7.0.4.3/lib/rails/commands/generate/generate_command.rb:26:in `perform'
    from /home/luka/.asdf/installs/ruby/3.2.1/lib/ruby/gems/3.2.0/gems/thor-1.2.1/lib/thor/command.rb:27:in `run'
    from /home/luka/.asdf/installs/ruby/3.2.1/lib/ruby/gems/3.2.0/gems/thor-1.2.1/lib/thor/invocation.rb:127:in `invoke_command'
    from /home/luka/.asdf/installs/ruby/3.2.1/lib/ruby/gems/3.2.0/gems/thor-1.2.1/lib/thor.rb:392:in `dispatch'
    from /home/luka/.asdf/installs/ruby/3.2.1/lib/ruby/gems/3.2.0/gems/railties-7.0.4.3/lib/rails/command/base.rb:87:in `perform'
    from /home/luka/.asdf/installs/ruby/3.2.1/lib/ruby/gems/3.2.0/gems/railties-7.0.4.3/lib/rails/command.rb:48:in `invoke'
    from /home/luka/.asdf/installs/ruby/3.2.1/lib/ruby/gems/3.2.0/gems/railties-7.0.4.3/lib/rails/commands.rb:18:in `<top (required)>'
    from <internal:/home/luka/.asdf/installs/ruby/3.2.1/lib/ruby/3.2.0/rubygems/core_ext/kernel_require.rb>:37:in `require'
    from <internal:/home/luka/.asdf/installs/ruby/3.2.1/lib/ruby/3.2.0/rubygems/core_ext/kernel_require.rb>:37:in `require'
    from /home/luka/.asdf/installs/ruby/3.2.1/lib/ruby/gems/3.2.0/gems/railties-7.0.4.3/lib/rails/engine/commands.rb:9:in `<top (required)>'
    from <internal:/home/luka/.asdf/installs/ruby/3.2.1/lib/ruby/3.2.0/rubygems/core_ext/kernel_require.rb>:37:in `require'
    from <internal:/home/luka/.asdf/installs/ruby/3.2.1/lib/ruby/3.2.0/rubygems/core_ext/kernel_require.rb>:37:in `require'
    from travel_admin/bin/rails:23:in `<main>'

Here is the database.yml file that's the same in main app's config/ folder and in the engine's config/ folder.


default: &default
  adapter: postgresql
  encoding: unicode
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

development:
  <<: *default
  database: db_name_development

test:
  <<: *default
  database: db_name_test

production:
  <<: *default
  database: db_name_production
  username: db_name
  password: <%= ENV["DATABASE_PASSWORD"] %>

staging:
  <<: *default
  database: db_name_staging
  username: db_name
  password: <%= ENV["DATABASE_PASSWORD"] %>

Upvotes: 0

Views: 135

Answers (1)

Greg
Greg

Reputation: 6659

Check your RAILS_ENV in the same console as you run that command:

echo $RAILS_ENV

if this doesn't show anything weird, check env inside the console:

rails c
[1] pry(main)> Rails.env
=> "development"

I checked locally and

ActiveRecord::Base.configurations.configs_for(env_name: "foo", name: "primary").configuration_hash

gives this exact error. Maybe you have a leftover

export RAILS_ENV=something-not-standard

in some of your rc files? (.bashrc, .zshrc ?).

Upvotes: 0

Related Questions