Forum: Ruby on Rails Multithreading problem, uninitialized constant in rake

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
4b42478d00bc698d04827f366b35f73a?d=identicon&s=25 Gerald Y. (gerald_y)
on 2013-07-31 06:21
Hi everyone,

I am having a problem with running rake tasks that involve threads.
Everytime I run it, I get an uninitialized constant with one of my
models (changes models occasionally) seems to be uninitialized.

I have tried to fix this by adding config.threadsafe! in the
application.rb. That did not work. So I tried to add
Rails.application.eager.load! and that did not work. I have tried to put
config.dependency_loading = true if $rails_rake_task but that does not
work. I have tried config.threadsafe! unless $rails_rake_task but that
did not work also.

I was wondering if anyone has come across this problem in which you are
using threads in a rake file and you get the uninitialized constant on
one of your models. Please help! I went through all the
stackoverflow pages related to this and nothing works :( I have ruby
1.9.3-p429, rails 3.2.13, rubygem 2.0.6
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2013-07-31 10:04
(Received via mailing list)
On Wednesday, July 31, 2013 5:21:56 AM UTC+1, User wrote:
> did not work also.

Loading classes is inherently a thread unsafe task - you run into all
sorts of race conditions. While rails has mechanisms for loading all
code upfront (it does that in production by default), and this is one of
the things that config.threadsafe! does. My recollection is that this
disabled for rake tasks though - I don't recall the precise reasons
though. If all else fails, you could use require_dependency to load all
of the classes you'll need ahead of time.

This topic is locked and can not be replied to.