我在生产模式下迁移数据库时遇到了一个奇怪的问题。
初始rake db:migrate
失败,错误“对象不缺少常量用户”,其中User是我的模型之一,在此迁移中可以像这样访问:
add_column :users, :feedbacks_count, :integer, :default => 0
User.reset_column_information
User.all.each do |u|
u.update_attribute :feedbacks_count, u.feedbacks.length
end
end
日志说明如下:
对象不会丢失常量用户!
/var/lib/gems/1.8/gems/activesupport-2.3.4/lib/active_support/dependencies.rb:417:in `load_missing_constant'
/var/lib/gems/1.8/gems/activesupport-2.3.4/lib/active_support/dependencies.rb:80:in `const_missing_not_from_s3_library'
/var/lib/gems/1.8/gems/aws-s3-0.6.2/lib/aws/s3/extensions.rb:206:in`const_missing'
/var/lib/gems/1.8/gems/activesupport-2.3.4/lib/active_support/dependencies.rb:92:in `const_missing'
/var/lib/gems/1.8/gems/activesupport-2.3.4/lib/active_support/dependencies.rb:98:in `发送“
/var/lib/gems/1.8/gems/activesupport-2.3.4/lib/active_support/dependencies.rb:98:in `const_missing'
./分贝/迁移// 20100823185519_add_columns_for_counter_caches.rb:5:在 `up_without_benchmarks'
/var/lib/gems/1.8/gems/activerecord-2.3.4/lib/active_record/migration.rb:282:in `发送“
/var/lib/gems/1.8/gems/activerecord-2.3.4/lib/active_record/migration.rb:282:in `迁移'
/usr/lib/ruby/1.8/benchmark.rb:293:in `衡量“
/var/lib/gems/1.8/gems/activerecord-2.3.4/lib/active_record/migration.rb:282:in `迁移'
/var/lib/gems/1.8/gems/activerecord-2.3.4/lib/active_record/migration.rb:365:in `的发送
/var/lib/gems/1.8/gems/activerecord-2.3.4/lib/active_record/migration.rb:365:in `迁移'
/var/lib/gems/1.8/gems/activerecord-2.3.4/lib/active_record/migration.rb:486:in `迁移'
/var/lib/gems/1.8/gems/activerecord-2.3.4/lib/active_record/migration.rb:562:in `称之为“
/var/lib/gems/1.8/gems/activerecord-2.3.4/lib/active_record/migration.rb:562:in `ddl_transaction'
/var/lib/gems/1.8/gems/activerecord-2.3.4/lib/active_record/migration.rb:485:in `迁移'
/var/lib/gems/1.8/gems/activerecord-2.3.4/lib/active_record/migration.rb:472:in `每个'
/var/lib/gems/1.8/gems/activerecord-2.3.4/lib/active_record/migration.rb:472:in `迁移'
/var/lib/gems/1.8/gems/activerecord-2.3.4/lib/active_record/migration.rb:400:in `向上“
/var/lib/gems/1.8/gems/activerecord-2.3.4/lib/active_record/migration.rb:383:in `迁移'
/var/lib/gems/1.8/gems/rails-2.3.4/lib/tasks/databases.rake:116
/var/lib/gems/1.8/gems/rake-0.8.7/lib/rake.rb:636:in`call'
/var/lib/gems/1.8/gems/rake-0.8.7/lib/rake.rb:636:in'execute'
/var/lib/gems/1.8/gems/rake-0.8.7/lib/rake.rb:631:in` each'
/var/lib/gems/1.8/gems/rake-0.8.7/lib/rake.rb:631:in'execute'
/var/lib/gems/1.8/gems/rake-0.8.7/lib/rake.rb:597:in`invoke_with_call_chain'
/usr/lib/ruby/1.8/monitor.rb:242:in `同步'
/var/lib/gems/1.8/gems/rake-0.8.7/lib/rake.rb:590:in`invoke_with_call_chain'
/var/lib/gems/1.8/gems/rake-0.8.7/lib/rake.rb:583:in“invoke”
/var/lib/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2051:in `invoke_task'
/var/lib/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2029:in `TOP_LEVEL'
/var/lib/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2029:in `每个'
/var/lib/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2029:in `TOP_LEVEL'
/var/lib/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2068:in `standard_exception_handling'
/var/lib/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2023:in `TOP_LEVEL'
/var/lib/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2001:in `运行'
/var/lib/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2068:in `standard_exception_handling'
/var/lib/gems/1.8/gems/rake-0.8.7/lib/rake.rb:1998:in `运行'
/var/lib/gems/1.8/gems/rake-0.8.7/bin/rake:31 / usr / bin / rake:19:在'load'
的/ usr /斌/耙:19
我读了很多文章,指的是“对象不缺少常量X”,其中问题主要是通过lazy_loading或访问插件来引用模型。
在访问users表之前,在add_column之前要求'user.rb'或添加User.new
并不能解决问题。
在发生故障之前访问用户模型也不会造成任何问题。
关于这个问题最奇怪的是,我在一天前将相同的代码迁移到另一台服务器,具有相同的设置和生产中,迁移运行没有任何问题。
任何帮助都会被贬低!
答案 0 :(得分:0)
当然,我不应该要求用户模型,而是将User类添加到Migration by
class User < ActiveRecord::Base; end
这就是所有的事情。
还有待检查的是,为什么用户模型不为移民所知....
答案 1 :(得分:0)
您的Amazon S3设置中的某些内容配置错误。你是否缺少配置目录中的S3配置文件?