Mongoid doesn't preload models in rake tasks

Jack Chu bio photo By Jack Chu Comment

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’s not just Heroku though. If I run that rake task locally, it will also just seem to freeze or lock up when it tries to access any Mongoid model. I’ve verified this with the ruby debugger.

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.

Update 10/12/2011 - Looks like I posted this too soon. This is coming back.

comments powered by Disqus