rake assets:precompile attempting to connect to database

前端 未结 6 846
余生分开走
余生分开走 2020-12-14 06:24

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

6条回答
  •  失恋的感觉
    2020-12-14 06:57

    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!

提交回复
热议问题