Lately, I have been using the next initializer in a Rails 5 project:
initializers/initialize_configs.rb
$current_city = Config.first.city
But after a few changes, I had to rails db:migrate and rails db:seed and then, I got this error. Until that point, my application worked well.
Config is a one-row only table in which I save configs for each VPS, and $current_city is also a var the each VPS has.
The problem is that this error show up when I do rails db:seed, and so I cant load my initial configuration.
How could I fix the problem while still loading this initial dalta?
When you run rails db:seed
then Rails is initialized, otherwise you were not able to use Rails models in that file. And that is the cause of your problem. Because in that initialization process Config.first.city
must fail because there are is no data in the database yet.
It is unclear why you need to load the app's configuration from the database. And others already pointed out that this is questionable and feel like a code smell.
Nevertheless, currently you have two options:
Config.first
is blank. For example by using reasonable defaults.