Uninitialized constant Rails
Closed this issue · 6 comments
Having
config.polling_environments << :production
config.sending_environments << :production
and
if Rails.env.production?
Localeapp::CLI::Pull.new.execute
end
in localeapp initializer,
trying to execute pull
command results in error: uninitialized constant Rails
.
While Rails.env.production
condition works perfectly in other parts of the app.
(Rails 5.)
hello,
# config/initializers/localeapp.rb
require 'localeapp/rails'
Localeapp.configure do |config|
config.api_key = ENV['LOCALEAPP_API_KEY']
config.polling_environments << :production
config.sending_environments << :production
end
if Rails.env.production?
puts Rails.env
Localeapp::CLI::Pull.new.execute
end
$ RAILS_ENV=production bundle exec rails s
=> Booting Puma
[…]
production
production
Localeapp Pull
Fetching translations:
Success!
Updating backend:
Success!
Localeapp Pull
Fetching translations:
Success!
Updating backend:
Success!
Puma starting in single mode...
[…]
I don't think rails initializers are meant for this usage, and using Localeapp::CLI::Pull
this way is not supported, but it seems to work: my translations got pulled.
Can you post the complete backtrace please?
If your goal is to pull translations during app boot, I would recommend this:
localeapp pull && bundle exec rails s
Or if it's important that pull is done while app boots:
localeapp pull &
bundle exec rails s
Replace localeapp pull
with bundle exec localeapp pull
if you need.
Well, the goal is to set a 'live' reload on Heroku, as described in the Docs, which works as intended, but in dev results in error above.
Oops, sorry I did not know about this usage in the docs. Let me do some more tests to reproduce, and I'll get back to you.
error: uninitialized constant Rails
is not from the rails app
context itself, but this is an error from localeapp
program when
used "independently" from the rails app:
# config/initializers/localeapp.rb
require 'localeapp/rails'
Localeapp.configure do |config|
config.api_key = ENV['LOCALEAPP_API_KEY']
end
Rails
$ localeapp add some_key 'en:some value'
error: uninitialized constant Rails
zsh: exit 1 localeapp add
Which is expected because nothing from the app (including rails) is
loaded at this point. A simple workaround would be to test if Rails
is defined:
# config/initializers/localeapp.rb
require 'localeapp/rails'
Localeapp.configure do |config|
config.api_key = ENV['LOCALEAPP_API_KEY']
config.polling_environments << :production
config.sending_environments << :production
end
if defined? Rails
if Rails.env.production?
puts Rails.env
Localeapp::CLI::Pull.new.execute
end
end
An alternative is to test the environment directly as described on the
wiki at the end of the section:
If you run in production somewhere else then you could set an
environment variable using something likeheroku config:set RUNNING_ON_HEROKU=1
and test for that rather than
Rails.env.production?
Environment variables will always be available, and we could test
RAILS_ENV
instead too.
I will discuss this with the development team, and see how we should
update the wiki.
Thank you for reporting this issue.
Which is expected because nothing from the app (including rails) is
loaded at this point.
That's a good point, @tjouan, thank you for the workaround!