I am unable to run migrations in Heroku, which I believe is due to a module I created in my lib directory. After executing the command heroku run rake db:migrate I receive the below error:
uninitialized constant ApplicationController::PgTools
/app/app/controllers/application_controller.rb:4:in <class:ApplicationController>
Line 4 of the Application controller is include PgTools, which is there to gain access to methods within the PgTools module I created.
Despite the heroku migration failing, I am able to run rake db:migrate in my local dev environments without fail (please note that both environments utilize postgres databases).
I also have the following two lines in my application.rb file
config.autoload_paths += %W(#{config.root}/lib)
config.autoload_paths += Dir["#{config.root}/lib/**/"]
I resolved the error by renaming Pg_Tools.rb to pgtools.rb and modifying all include PgTools statements to include Pgtools
Links that I used in the troubleshooting process are shown below
Rails 3 library not loading until require
http://www.williambharding.com/blog/technology/rails-3-autoload-modules-and-classes-in-production/
Related
I have a Rails 5 application with some modules/classes located under /lib. In development I can access those through the rails console like so:
irb(main):001:0> MyClass.do_something
In Production on Heroku I get this:
irb(main):001:0> MyClass.do_something
NameError: uninitialized constant MyClass
As you might have guessed I autoload the /lib directory in my application.rb:
config.autoload_paths << Rails.root.join('lib')
However, the most curious thing about this is, that I can access this class from rake tasks. So something like this works fine:
task do_something: :environment do
MyClass.do_something
end
Which tells me that the class is present on Heroku.
Any ideas?
Rails doesn't autoload in production for thread safety, instead eager loading application constants. You can fix your issue by using the eager_load_paths method instead.
config.eager_load_paths << Rails.root.join('lib')
If you still want to autoload in development you can make it conditional
load_path_strategy = Rails.env.production? ? :eager_load_paths : :autoload_paths
config.public_send(load_path_strategy) << Rails.root.join('lib')
If you really need autoloading of this directory in production you can set enable_dependency_loading to true.
config.enable_dependency_loading = true
config.autoload_paths << Rails.root.join('lib')
See this blog post for more explanation.
I have been puzzled why all of my objects were uninitialized constant in console on heroku in production. In my local production, they were fine.
Turns out that the problem was that I was running: "heroku run console" not "heroku run rails console".
Worth noting that when you access console from the heroku website, the same problem occurs there too. Wasted a lot of time on this.
New rails project.
Rails 5.0.2
Sidekiq 4.2.10
I ran rails g sidekiq:worker deposit_collector and then filled out the code I needed inside the perform method.
To test I logged into the rails console and typed: DepositCollector.perform_async and I get the error:
NameError: uninitialized constant DepositCollectorWorker
The worker is where it should be in the app/workers/ folder. I've used sidekiq on several projects before and have never run into this.
By default, Rails will include all subdirectories of the app folder in the autoload paths list. You can review the list of autoload paths in the console with:
puts ActiveSupport::Dependencies.autoload_paths
But Rails only looks for these paths at boot time. So when you add a new folder, like app/workers, it is not enough to restart the Rails console. You need to both exit the Rails console and stop the Spring Application Preloader with:
spring stop
Then start up the Rails console again and the files in the app/workers folder will load properly.
I didn't have spring installed so I took the lazy approach and turned on eager loading in my config/environments/development.rb file:
config.eager_load = true
and then turned it back to false after restarting my server.
I am pretty much brand new to RoR and such.
I am following a video tutorial to build my own web-based app, and I got to the step:
git push heroku master
When in git bash, it was coming up with an error that claimed it couldn't compile ruby. Now, it says it is launched and deployed, but there is still the same error on the page for my app, http://infinite-mountain-6131.herokuapp.com/
Any ideas?? I can add files if needed.
Requested file(s):
app/config/application.rb from my comment
require File.expand_path('../boot', __FILE__)
require 'rails/all'
# config/application.rb
config.assets.initialize_on_precompile = false
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)
module Myrubyblog
class Application < Rails::Application
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
# config.time_zone = 'Central Time (US & Canada)'
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
# config.i18n.default_locale = :de
end
end
Line 6 that was mentioned in my error is the config.assets.initialize which I put in there with line 5 as suggested to fix my problem.
This is what happens when I run the migrate as suggested (heroku run rake db:migrate)
Running 'rake db:migrate' attached to terminal... up, run.6274
rake aborted!
NameError: undefined local variable or method 'config' for main:Object
/app/config/application.rb:6:in '<top <required>>'
/app/Rakefile:4:in 'require'
/app/Rakefile:4:in '<top <required>'
<See full trace by running task with --trace>
As a rule, there are two types of error you can get when hosting a Rails app on Heroku:
Heroku Error
-
Rails Error
--
Error
The difference between the two is important - rails errors will only occur if your operating environment is actually "running" your Rails application. Heroku errors will occur if your operating environment / Heroku will not load correctly
The problem you have is definitely a Heroku issue - one which is typically created by a lack of db connectivity. The way to fix this issue is to ensure your application has all the necessities to run - most notably the correct db
You'll be best using the following:
$ heroku run rake db:migrate
However, I appreciate this won't be the only issue you'll have
Heroku Deployment
As you've said you're a "beginner" to ROR, let me give you some ideas
Firstly, when you write a question on here, it helps to divulge as much information as possible - typically from the logs, or any other specific error handling mechanism
Secondly, you want to ensure that everything required to get your application running has been achieved. Most notably, when you mention Heroku cannot compile the Ruby application, you'll need to provide information on why this is the case -- there'll probably be a gem conflict (SQLite3) or similar
Thirdly, you need to ensure you have migrated your database. This is the single biggest reason why "Heroku errors" appear - deploying your Rails app doesn't mean the migrations you made locally will persist - you need to ensure you have the db updated as you require, which can be done as follows:
$ heroku run rake db:migrate
I am trying to precompile the assets while deploying to production environment. I am also trying to automate it for first time installation on a server using capistrano. It looks like none of the initializer's are getting loaded during the assets precompile process.
I am facing these two problems
For the first time installation I am generating a initializer file(initializers/freshinstall.rb) on the fly with the below content
config.assets.initialize_on_precompile = false
so that precompile doesn't check the database which doesn't exist yet
I also have some vendor files, and their locations are set in the asset pipeline, and are placed in separate initializer file initializers/vendor.rb
MyApp::Application.config.assets.paths << "#Rails.root}/vendor/assets/images/xxxx" << "# {Rails.root}/vendor/assets/images/xxxx/helpers"
MyApp::Application.config.assets.paths << "#{Rails.root}/vendor/assets/stylesheets/xxxx"
MyApp::Application.config.assets.paths << "#{Rails.root}/vendor/assets/stylesheets/yyyy" << "#{Rails.root}/vendor/assets/images/yyyy"
When capistrano runs the assets precompile task, its not able to find the vendor paths or stopping it from looking into the database. This brings me to a conclusion that the initializer's are not getting loaded. After going through some stackoverflow questions. I even added a railtie to config/application.rb
module AssetsInitializers
class Railtie < Rails::Railtie
initializer "assets_initializers.initialize_rails",
:group => :assets do |app|
require "#{Rails.root}/config/initializers/freshinstall.rb"
require "#{Rails.root}/config/initializers/vendor.rb"
end
end
end
But I still don't see any initializers getting loaded. Can I get some info on the internals of the boot process of an rail application, and also why the initializers are not getting loaded during the assets precompile process.
Some documentations would be really helpful to understand this. The Rails documentation is very minimal with respect to railtie and the initializer method.
http://guides.rubyonrails.org/configuring.html
Thank you in advance
Finally was able to figure out how to do it. This works for my single server capistrano deployment. Below is the capistrano task for my new_deploy on to a fresh server.
set :fresh_install, false
task :new_deploy do
set :fresh_install,true
deploy.setup
#The assets:precompile process is part of the deploy.update. Before the precompile process, we will create database.
deploy.update
deploy.migrate
run_seed
#load unicorn server
end
Dont do any configuration setting mentioned below
config.assets.initialize_on_precompile = false # no need of this in application.rb
Instead just before assets:precompile create your database
before "deploy:assets:precompile" , "yourapp:create_database"
See that you check if the fresh install flag is set, then only create the database.This method will also be called during regular deploy when ever you are updating the server with latest revision. During that scenario fresh_install flag will be false.
desc 'Create a new database'
task :create_database, :roles => :app do
if fresh_install
run "cd #{release_path}; bundle exec rake db:create RAILS_ENV=#{rails_env}"
end
end
Rails 3.2.2
When running rake assets:precompile I get the following error:
uninitialized constant Redirect
Redirect is a custom middleware class that redirects naked domain requests from mydomain.com to www.mydomain.com.
I load the middleware in production.rb using:
config.middleware.use Redirect
The redirect.rb is located at lib/middleware/redirect.rb. I load the path in application.rb using:
config.autoload_paths += %W(#{config.root}/lib/middleware)
It works fine when you run the application, and other rake tasks run fine. But running rake assets:precompile appears to not load the lib properly. I first noticed the issue running on Heroku, but I've been able to reproduce locally no problem.
Any ideas? Thanks!
You probably have config.assets.initialize_on_precompile = false set somewhere.
I experienced this error after setting that configuration for something related to Heroku. To fix, i just require "#{Rails.root}/lib/my_middleware.rb" right above the line where I configure the app to use the middleware.
I was getting the same error for loading a class from /lib and assigning it to a ::GLOBAL variable.
This was because I had forgotten to place it inside an after_initialize block, which is how I had done it in development.
config.after_initialize do
::GLOBAL = MyLib::MyClass.new
end
Hope this helps somebody!