Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cannot load 'rails.application.database_configuration' undefined method'[]' for nil:NilClass

I changed my database.yml to use sqlite3 database in test and developement and postgresql in production. My application run fine in production but when I launch test or development environements i have this error :

Cannot load 'Rails.application.database_configuration':
undefined method'[]' for nil:NilClass (NoMethoError)

my database.yml:

# SQLite version 3.x
#   gem install sqlite3
#
#   Ensure the SQLite 3 gem is defined in your Gemfile
#   gem 'sqlite3'
#
default: &default
  adapter: sqlite3
  pool: 5
  timeout: 5000

development:
  <<: *default
  database: db/development.sqlite3

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
  <<: *default
  database: db/test.sqlite3

production:
  pool: 5
  timeout: 5000
  encoding: utf8
  adapter: postgresql
  host: <%= Rails.application.secrets[:database][:host]%>
  database: <%= Rails.application.secrets[:database][:name]%>
  username: <%= Rails.application.secrets[:database][:username]%>
  password: <%= Rails.application.secrets[:database][:password]%>
like image 575
scauglog Avatar asked Nov 10 '22 18:11

scauglog


1 Answers

Ran into this with the new Rails 5.1.0 with adding my variables in with the new secrets encrypted file.

Your nil is happening because even though it is development it still tries to load everything and you are calling things from nil -> something[:nil][:cant_grab_from_nil]. The quick workaround is an if statement. Only suggest this when using the secrets encrypted file in the new Rails 5.1.

production:
  pool: 5
  timeout: 5000
  encoding: utf8
  adapter: postgresql
  <% if Rails.application.secrets[:database].present? %>
    host: <%= Rails.application.secrets[:database][:host]%>
    database: <%= Rails.application.secrets[:database][:name]%>
    username: <%= Rails.application.secrets[:database][:username]%>
    password: <%= Rails.application.secrets[:database][:password]%>
  <% end %>
like image 107
Jake Avatar answered Nov 29 '22 15:11

Jake