I\'m attempting to debug why my application is attempting to connect to my database when I run rake assets:precompile --trace
.
I\'m probably missing so
Maybe this won't work for all cases but I was able to avoid Rails attempting to connect to the database on the rake assets:*
tasks by surrounding the code in some places with the snippet below on the /config/routes.rb
:
unless defined?(::Rake::SprocketsTask)
devise_for ...
#...
end
devise_scope ...
#...
end
end
Sometimes code which depends on external services or environment variables will get loaded because of code on the initializer files which are not required for assets to be precompiled.
I've recently faced a similar issue and the responses above didn't work for me. I've found the file on config/initializers/
that was causing the issue and wrapped it with the following code:
unless defined?(::Rake::SprocketsTask)
#...
end
The snippet above will skip the code within it when running rake assets:precompile
or others like rake assets:clean
as well.
Hopefully this will help others in the future!
Check your configuration files (in all environments). If for example
rake assets:precompile
works in development, but you are having issues in production
RAILS_ENV=production bundle exec rake assets:precompile
then you most likely have a reference to active record in your /config/environments/production.rb
config.active_record.dump_schema_after_migration = false
In order to avoid that altogether if you are planning on not taking advantage of active record or employing a NoSQL such as mongo, you may wish to initialize your new app as follows:
rails new myApp --skip-active-record
which is equivalent to -O (capital O) as far as I am concerned.
Newer versions of Rails do not load the database during asset precompile. However, initializers will often cause it to load. This can be frustrating to debug, because the error omits the stack trace.
./bin/rails assets:precompile
(in ~/src/nautilus)
rake aborted!
ActiveRecord::NoDatabaseError: FATAL: database "your_app_development" does not exist
bundler: failed to load command: rake (~/src/your_app/vendor/bundle/ruby/2.6.0/bin/rake)
NoMethodError: undefined method `reject' for nil:NilClass
The less than helpful error.
So, how do we figure out where the problem is?
Move all the initializers out of config/initializers
then run ./bin/rails assets:precompile
.
If it works, great, you know the problem is in one of those files.
Move them back one by one till you find the offending file(s).
Now that the problem was found, how do we fix it?
Then when I find an initialilzer that has a problem I wrap it with the following check:
unless ARGV.include? "assets:precompile"
# ...
end
I prefer this over the nulldb because it reduces the number of dependencies I have to maintain in the app.
rake assets:precompile
initializes your app by default, which includes connection to the database.
Inside config/application.rb
you can add this, but see the link below for warnings about it:
config.assets.initialize_on_precompile = false
Rails Guide on Precompiling Assets
If it makes sense in your situation, you can choose against which environment assets:precompile
should work, with the following command:
rake assets:precompile:all RAILS_ENV=development RAILS_GROUPS=assets
This make sense for my deployment because usually:
Hope it helps.
I had that same problem. After updating Sprockets to version 3, whenever I tried to precompile the assets locally (development), however using the settings of the production environment, I had this error:
rake aborted! Gem::LoadError: Specified 'postgresql' for database adapter, but the gem is not loaded. Add
gem 'pg'
to your Gemfile (and ensure its version is at the minimum required by ActiveRecord).
Because in my local (development) I use MySQL and in the server (production), I use Postgres.
The answer marked as resolved does not work for me, because config.assets.initialize_on_precompile
is not available in Rails 4.2.1
.
To solve, I followed 3 simple steps:
gem "activerecord-nulldb-adapter"
In database.yml
, change the adapter as follow:
production:
adapter: <%= ENV['DB_ADAPTER'] ||= 'postgresql' %>
To compile your production assets locally. run in your terminal
DB_ADAPTER=nulldb RAILS_ENV=production rake assets:precompile
This solutions solved to me and I sawyed here.