I have a Rails application with an initializer that accesses my Mongoid models. This works fine locally when I run rails s, but when I deploy to Heroku, the deployment will eventually timeout as Heroku tries to run:
rake assets:precompile

It turns out that Mongoid only preloads your models when $rails_rake_task is false (ie. not a rake task). Here’s the Github issue where this change was made. I’m not sure why this was changed. I guess most rake tasks don’t need to eager load the models since they don’t need to access them. To work around this, I’ve just put a condition also checking for$rails_rake_task. In my case, the initializers need to access the models when the app starts up, not when rake tasks are run.