# Migration erros when truning on config.threadsafe!

**URL:** <https://www.ruby-forum.com/t/migration-erros-when-truning-on-config-threadsafe/174336>\
**Category:** Rails\
**Created:** [August 25, 2009, 9:16am UTC](https://www.ruby-forum.com/t/migration-erros-when-truning-on-config-threadsafe/174336 "2009-08-25T09:16:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![khelll](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/dec6dc/32.png) [@khelll](https://www.ruby-forum.com/u/khelll)\
**Post date:** [August 25, 2009, 9:16am UTC](https://www.ruby-forum.com/t/migration-erros-when-truning-on-config-threadsafe/174336/1 "2009-08-25T09:16:54Z")

</div>

When turning on config.threadsafe! in production environment and  
trying to migrate production database I get odd errors like  
uninitialized constant AddAdminToUsers::User, here is the migration  
file data:

class AddAdminToUsers \< ActiveRecord::Migration  
def self.up  
User.create!(:username=\>  
‘admin’,:first\_name=\>“admin”,:last\_name=\>“admin”, :password=\>  
‘admin@exam$’,:password\_confirmation=\> ‘admin@exam$’)  
end

def self.down  
end  
end

This error never shows up when that setting is off…

---

<div class="post-metadata">

**Author:** ![khelll](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/dec6dc/32.png) [@khelll](https://www.ruby-forum.com/u/khelll)\
**Post date:** [August 25, 2009, 7:11pm UTC](https://www.ruby-forum.com/t/migration-erros-when-truning-on-config-threadsafe/174336/2 "2009-08-25T19:11:09Z")

</div>

Ok just for the record, i found a similar thread regarding that here  
[https://rails.lighthouseapp.com/projects/8994/tickets/2506-models-are-not-loaded-in-migrations-when-configthreadsafe-is-set](https://rails.lighthouseapp.com/projects/8994/tickets/2506-models-are-not-loaded-in-migrations-when-configthreadsafe-is-set)  
, and all I did to fix it is to require the user model at the top of  
this migration file:

require ‘user’

---

<div class="post-metadata">

**Author:** ![khelll](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/dec6dc/32.png) [@khelll](https://www.ruby-forum.com/u/khelll)\
**Post date:** [August 25, 2009, 7:27pm UTC](https://www.ruby-forum.com/t/migration-erros-when-truning-on-config-threadsafe/174336/3 "2009-08-25T19:27:33Z")

</div>

I’ll just throw out the notion that this may not be the right solution.

In general, if you actually _use_ a model to do something in a  
migration, you should define a minimal version of the model _inside_  
the migration class. For example:

class AddAdminToUsers \< ActiveRecord::Migration  
class User \< ActiveRecord::Base; end  
def self.up  
User.create!(:username=\>‘admin’,  
:first\_name=\>“admin”, :last\_name=\>“admin”,  
:password=\>‘admin@exam$’,  
:password\_confirmation=\>‘admin@exam$’)  
end

def self.down  
end  
end

That way changes to the model or the database don’t lead to odd  
failures later on – like when a production deployment is several  
migration versions behind and tries to do some now incompatible things  
with an earlier migration. Think what would happen if the ‘username’  
column were to be renamed to ‘login’ in a later migration and both  
needed to be applied.

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid
