English 中文(简体)
undefined method info for nil:NilClass when running active record migration
原标题:

I am trying to run an active record migration but am receiving the following error:

undefined method info for nil:NilClass

Here is the 2 lines of code in my rake task that runs the migration

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 )

And here is my only migration class in the folder

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

And here is the trace statement:

** Invoke migrate (first_time)
** Execute migrate
rake aborted!
undefined method info for nil:NilClass<br /> C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/migration.rb :473:inmigrate
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/migration.rb :472:in each <br /> C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/migration.rb :472:inmigrate
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/migration.rb :400:in up <br /> C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/migration.rb :383:inmigrate
E:/Working/Code/WMF/SupporterSync/rakefile.rb:19
C:/Ruby/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:636:in call <br /> C:/Ruby/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:636:in execute
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 execute
C:/Ruby/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:597:in invoke_with_call_c hain <br /> C:/Ruby/lib/ruby/1.8/monitor.rb:242:in synchronize
C:/Ruby/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:590:in invoke_with_call_c hain <br /> C:/Ruby/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:583:in invoke
C:/Ruby/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2051:in 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:in 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:in 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:inload
C:/Ruby/bin/rake:19

最佳回答

I suggest following Rails conventions and making your table + field names lowercase_and_underscored rather than CamelCasing. Some macros such as belongs_to look for the lowercase variants and by using them you make your life a lot easier. I m sorry this does not quite answer your question.

Also, the primary key is "id" by default, so you don t need to set it.

Thirdly, migrations are usually run with rake db:migrate.

Lastly, the reason you re getting that error is that you re setting up ActiveRecord::Base and not defining a logger object on it, like Rails does for you when you run any task descending from :environment. For more information, see this line in the Rails source.

问题回答

I turned off active_record logger (config.active_record.logger = nil) and came across this problem. I solved it by adding a logger back:

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

I added the code to config/environment/xxx.rb.





相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签