nil的未定义方法'info':运行活动记录迁移时的NilClass

时间:2009-11-12 01:07:33

标签: ruby-on-rails activerecord

我正在尝试运行活动记录迁移,但收到以下错误:

  

nil的未定义方法'info':NilClass

以下是我的rake任务中运行迁移的两行代码

ActiveRecord::Base.establish_connection(YAML::load(File.open('src/SupporterSync.Core/Database/Database.yml')))
ActiveRecord::Migrator.migrate('src/SupporterSync.Core/Database/Migrations', ENV["VERSION"] ? ENV["VERSION"].to_i : nil )

这是我在文件夹

中唯一的迁移类
class InitialMigration < ActiveRecord::Migration
  def self.up
    create_table :Accounts, :primary_key => :Id do |t|
      t.string :ListId, :limit => 36, :null => false
      t.string :Name, :limit => 31, :null => false
      t.string :FullName, :limit => 31, :null => false
      t.string :ParentListId, :limit => 36
    end
  end
  def self.down
    drop_table :Accounts
  end
end

这是跟踪声明

  

**调用migrate(first_time)
  **执行迁移耙中止!
未定义方法info' for nil:NilClass<br /> C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/migration.rb :473:in迁移'
  C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/migration.rb   :472:在each'<br /> C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/migration.rb :472:in迁移中   C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/migration.rb   :400:up'<br /> C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/migration.rb :383:in迁移'   E:/Working/Code/WMF/SupporterSync/rakefile.rb:19个
  C:/Ruby/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:636:在call'<br /> C:/Ruby/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:636:in 执行'
  C:/Ruby/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:631:in each'<br /> C:/Ruby/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:631:in 执行'
  C:/Ruby/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:597:在   invoke_with_call_c hain'<br /> C:/Ruby/lib/ruby/1.8/monitor.rb:242:in 同步'
  C:/Ruby/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:590:在   invoke_with_call_c hain'<br /> C:/Ruby/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:583:in 调用'
  C:/Ruby/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2051:在   invoke_task'<br /> C:/Ruby/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2029:in TOP_LEVEL'
  C:/Ruby/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2029:在   each'<br /> C:/Ruby/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2029:in TOP_LEVEL'
  C:/Ruby/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2068:在   standard_exceptio n_handling'<br /> C:/Ruby/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2023:in TOP_LEVEL'
  C:/Ruby/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2001:in run'<br /> C:/Ruby/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2068:in standard_exceptio n_handling'
  C:/Ruby/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:1998:in run'<br /> C:/Ruby/lib/ruby/gems/1.8/gems/rake-0.8.7/bin/rake:31<br /> C:/Ruby/bin/rake:19:in load'
C:/ Ruby / bin / rake: 19个

2 个答案:

答案 0 :(得分:5)

我建议遵循Rails的约定并使你的表+字段名称为lowercase_and_underscored而不是CamelCasing。像belongs_to这样的一些宏寻找小写变体,使用它们可以让你的生活更轻松。对不起,这不能完全回答你的问题。

此外,默认情况下,主键为“id”,您无需进行设置。

第三,迁移通常使用rake db:migrate运行。

最后,您收到该错误的原因是因为您正在设置ActiveRecord::Base而没有在其上定义记录器对象,就像Rails在您运行{{1 }}。有关更多信息,请参阅Rails源代码中的see this line

答案 1 :(得分:1)

我关闭了active_record logger(config.active_record.logger = nil)并遇到了这个问题。我通过添加记录器来解决它:

config.active_record.logger=Logger.new(STDOUT)

我将代码添加到config / environment / xxx.rb。