4

I'm deploying my Rails app on Heroku (Cedar), and there were 3 options about precompiling my assets I could choose from, and I chose the option where Heroku precompiles my assets on deployment.

When I pushed, I got an error that it cannot access my database (during precompiling). So, how to make Rails not connect to the database during precompiling? I don't know why it's set in the first place, because I can't imagine a scenario where precompiling would need access to the database.

I saw somewhere a solution to disable initializing the application on precompiling, which is achieved by adding the following into the application.rb (setting it in the environments/production.rb doesn't work):

config.assets.initialize_on_precompile = false

I tried adding this line, and it works, but I don't know if it is a good solution. Wouldn't this make some plugins you would potentially use for the assets not load during precompiling, thus affecting the end result?

Janko
  • 8,985
  • 7
  • 34
  • 51

2 Answers2

1

What you're doing is the correct way. If you don't use models / anything else which is actually accessing the database in your assets then you have no need for it. The only time you'd need to have your app initialized is if you were doing things like this: (Completely contrived example, but you can see what I'm getting at)

/* In some css file */
.some_class{
    #{User.find(1).avatar_url}
}
Mal Curtis
  • 661
  • 7
  • 13
0

If you enable Heroku Labs (http://devcenter.heroku.com/articles/labs-user-env-compile) you can have access to your Db at deployment time which works great.

Do you use Devise? That's usually a culprit for DB access on precompiling assets incidentally.

John Beynon
  • 37,398
  • 8
  • 88
  • 97
  • No, I don't use Devise. Thanks for the suggestion, but I want to solve it by disabling the access to the database, because I don't need it. Also, if I learn how to do that, maybe I will also learn how to change some other things about precompiling, which will be a really useful knowledge :) – Janko Feb 05 '12 at 21:48
  • I'm using CarrierWave. Do you think it might have something to do with it? – Janko Feb 05 '12 at 23:03
  • ...but I'm going to accept, since it's a lot better solution than any I've seen so far :) – Janko Feb 06 '12 at 10:57