db:seed not loading models
I'm trying to 开发者_如何学编程seed my database with the standard db/seeds.rb
method. This works fine on my development machine, but on my server, I get:
$ sudo rake db:seed RAILS_ENV=production --trace
** Invoke db:seed (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute db:seed
rake aborted!
uninitialized constant Permission
/usr/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2503:in `const_missing'
/usr/local/lib/ruby/gems/1.8/gems/activesupport-2.3.4/lib/active_support/dependencies.rb:92:in `const_missing'
/path/.../.../.../.../db/seeds.rb:4
/usr/local/lib/ruby/gems/1.8/gems/rails-2.3.4/lib/tasks/databases.rake:215:in `load'
/usr/local/lib/ruby/gems/1.8/gems/rails-2.3.4/lib/tasks/databases.rake:215
/usr/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:636:in `call'
...
But when I check in the console, the model does exist:
$ script/console production
Loading production environment (Rails 2.3.4)
>> Permission
=> Permission(id: integer, ..., created_at: datetime, updated_at: datetime)
What am I forgetting?
In a comment to the blog posted linked to above and here again: http://www.builtfromsource.com/2011/02/09/getting-rake-dbseed-and-config-threadsafe-to-play-nice/
Bruce Adams mentions that one can call:
config.threadsafe! unless $rails_rake_task
to only turn on threadsafe when not running a rake task.
But as the issue is really that threadsafe turns off dependency_loading, you can simply add this line after config.threadsafe! to leave it enabled, but still load your environment as you expect.
config.dependency_loading = true if $rails_rake_task
It can be fixed by disabling threadsafe!
in the environment configuration.
I just ran across a good approach to this problem in this article. I'll summarize here so people can (hopefully) find it quicker.
The idea is to turn off threadsafe in the production environment, first by editing config/environments/production.rb:
config.threadsafe! unless ENV['THREADSAFE'] == 'off'
You then set THREADSAFE=off when running rake tasks.
精彩评论