rake assets:precompile attempting to connect to database

徘徊边缘 提交于 2019-11-30 02:47:43
Dylan Markow

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

danidemi

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:

  1. I make rake to generates assets on my development machine (because the memory of my vps is somehow limited)
  2. I zip all the application with the assets generated in public/assets
  3. I transfer the zip to the vps ans unzip the package there

Hope it helps.

DR.Somar

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:

  1. In your Gemfile, add: gem "activerecord-nulldb-adapter"
  2. In database.yml, change the adapter as follow:

    production:
      adapter: <%= ENV['DB_ADAPTER'] ||= 'postgresql' %>
    
  3. 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.

Georg Keferböck

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!