What exactly was initialize_on_precompile used for?
Asked Answered
S

1

8

I'm having trouble understanding what initialize_on_precompile was used for. What exactly did it mean for it to be false (especially as it relates to Heroku)? What did it mean for it to be true?

Apologies if this question is too broad/vague, but I can't seem to find the answer anywhere.

Stevestevedore answered 9/1, 2017 at 1:17 Comment(3)
The very first link when searching "initialize_on_precompile heroku" on Google tells you exactly what it does devcenter.heroku.com/articles/rails-asset-pipelinePlacard
Well, I just wanted a comprehensive answer for what it means. How do I know it doesn't trigger other things to happen? Again, apologies for the newb question.Stevestevedore
@Placard This person is clearly a newb. He even apologized in the original post. Sheesh. No use being so harsh. I'm sure someone else would have appreciated a better explanation of the configuration beyond 2 sentences. Everyone starts somewhere. This community used to be a lot of more welcoming 6 years ago.Grandfather
M
10

This option was available till Rails 3.x as when you run rake assets:precompile it initializes the application and tries to connect to the database. So setting this option to false prevents it. So if you have any issues in connection to the database rake assets:precompile won't work and fail this option ensures that it will work.

From Rails Git Repo:

The initialize_on_precompile change tells the precompile task to run without invoking Rails. This is because the precompile task runs in production mode by default, and will attempt to connect to your specified production database. Please note that you cannot have code in pipeline files that relies on Rails resources (such as the database) when compiling locally with this option.

In Rails 4.x this option has been removed and is no longer required. Rails 4 now always loads initializers and the database configuration before precompiling assets

Source of Commit: https://github.com/rails/rails/commit/2d5a6de

Muddler answered 9/1, 2017 at 1:38 Comment(3)
Thanks for the quick answer! What does this mean for gems that required initialize_on_precompile to be false on Rails 3? For example, the newest version of devise is now Rails 5-compatible. Does that mean it's changed to take into consideration that initializers will always run before precompiling assets?Stevestevedore
Yes it is true that the gems which are compatible with rails 4 and higher have taken this into consideration.Muddler
Many thanks for your patience. This really helped me understand what might be going on.Stevestevedore

© 2022 - 2024 — McMap. All rights reserved.