迁移问题,Active Record中的“未定义方法`ext'”?

时间:2014-01-08 13:38:45

标签: ruby-on-rails ruby ruby-on-rails-3 migrate railsinstaller

我使用railsinstaller安装了它:

C:\Sites>ruby -v
ruby 1.8.7 (2010-12-23 patchlevel 330) [i386-mingw32]

C:\Sites>rails -v
Rails 3.0.3

C:\Sites>gem -v
1.3.7

我使用railinstaller来安装Ruby on Rails。起初我安装了3.0.0版本的Ruby和Rails的更新版本,但是,因为我想使用一些Rails 3.1.0代码,我卸载了v 3.0.0并安装了其他版本,但结果很糟糕。我甚至无法运行rails server所以我再次尝试,只有这个版本有效。

我正在运行“敏捷Web开发”第6章中的示例:

  1. 使用:rails new depot
  2. 创建Rails应用程序
  3. 使用以下方法生成脚手架:

    rails generate scaffold Product\
    title:string description:text image_url:string price:decima
    
  4. 通过添加十进制长度来更改create_product.rb。

  5. 我准备好了一切,但当我跑rake db:migrate时,我明白了:

    C:\Sites\depot>rake db:migrate
    (in C:/Sites/depot)
    ==  CreateProducts: migrating =================================================
    -- create_table(:products)
    rake aborted!
    An error has occurred, this and all later migrations canceled:
    
    undefined method `ext' for #<ActiveRecord::ConnectionAdapters::TableDefinition:0
    x599f4c0>
    
    (See full trace by running task with --trace)
    

    以下是完整的跟踪:

    C:\Sites\depot>rake db:migrate --trace
    (in C:/Sites/depot)
    ** Invoke db:migrate (first_time)
    ** Invoke environment (first_time)
    ** Execute environment
    ** Execute db:migrate
    ==  CreateProducts: migrating =================================================
    -- create_table(:products)
    rake aborted!
    An error has occurred, this and all later migrations canceled:
    
    undefined method `ext' for #<ActiveRecord::ConnectionAdapters::TableDefinition:0x4c4d618>
    F:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract/schema_definitions.rb:326:in `method_missing'
    C:/Sites/depot/db/migrate/20140108105456_create_products.rb:5:in `up_without_benchmarks'
    F:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract/schema_statements.rb:157:in `create_table'
    F:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activerecord-3.0.3/lib/active_record/migration.rb:383:in `send'
    F:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activerecord-3.0.3/lib/active_record/migration.rb:383:in `method_missing'
    F:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activerecord-3.0.3/lib/active_record/migration.rb:359:in `say_with_time'
    F:/RailsInstaller/Ruby1.8.7/lib/ruby/1.8/benchmark.rb:293:in `measure'
    F:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activerecord-3.0.3/lib/active_record/migration.rb:359:in `say_with_time'
    F:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activerecord-3.0.3/lib/active_record/migration.rb:379:in `method_missing'
    C:/Sites/depot/db/migrate/20140108105456_create_products.rb:3:in `up_without_benchmarks'
    F:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activerecord-3.0.3/lib/active_record/migration.rb:312:in `send'
    F:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activerecord-3.0.3/lib/active_record/migration.rb:312:in `migrate'
    F:/RailsInstaller/Ruby1.8.7/lib/ruby/1.8/benchmark.rb:293:in `measure'
    F:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activerecord-3.0.3/lib/active_record/migration.rb:312:in `migrate'
    F:0:in `__send__'
    F:0:in `migrate'
    F:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activerecord-3.0.3/lib/active_record/migration.rb:537:in `migrate'
    F:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activerecord-3.0.3/lib/active_record/migration.rb:611:in `call'
    F:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activerecord-3.0.3/lib/active_record/migration.rb:611:in `ddl_transaction'
    F:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract/database_statements.rb:139:in `transaction'
    F:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activerecord-3.0.3/lib/active_record/transactions.rb:204:in `transaction'
    F:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activerecord-3.0.3/lib/active_record/migration.rb:611:in `ddl_transaction'
    F:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activerecord-3.0.3/lib/active_record/migration.rb:536:in `migrate'
    F:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activerecord-3.0.3/lib/active_record/migration.rb:523:in `each'
    F:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activerecord-3.0.3/lib/active_record/migration.rb:523:in `migrate'
    F:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activerecord-3.0.3/lib/active_record/migration.rb:433:in `up'
    F:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activerecord-3.0.3/lib/active_record/migration.rb:415:in `migrate'
    F:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activerecord-3.0.3/lib/active_record/railties/databases.rake:142
    F:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:636:in `call'
    F:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:636:in `execute'
    F:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:631:in `each'
    F:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:631:in `execute'
    F:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:597:in `invoke_with_call_chain'
    F:/RailsInstaller/Ruby1.8.7/lib/ruby/1.8/monitor.rb:242:in `synchronize'
    F:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:590:in `invoke_with_call_chain'
    F:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:583:in `invoke'
    F:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2051:in `invoke_task'
    F:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2029:in `top_level'
    F:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2029:in `each'
    F:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2029:in `top_level'
    F:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2068:in `standard_exception_handling'
    F:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2023:in `top_level'
    F:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2001:in `run'
    F:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2068:in `standard_exception_handling'
    F:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:1998:in `run'
    F:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/rake-0.8.7/bin/rake:31
    F:/RailsInstaller/Ruby1.8.7/bin/rake:19:in `load'
    F:/RailsInstaller/Ruby1.8.7/bin/rake:19
    

    我是Ruby on Rails的新手,而不是如何解决这个bug。我怎么解决这个问题?非常感谢!

3 个答案:

答案 0 :(得分:2)

我已经完成了所提供的所有步骤,似乎你有语法错误。 这是迁移代码:

class CreateProducts < ActiveRecord::Migration
 def self.up
  create_table :products do |t|
   t.string :title
   t.text :description
   t.string :image_url
   t.decimal :price

   t.timestamps
  end

 end

 def self.down
  drop_table :products
 end
end

在第5行存储说明中,您似乎添加了空格或已移除t.t

使用以下代码查看该行:

t.text :description

答案 1 :(得分:1)

看起来您在此文件中有错误:

C:/Sites/depot/db/migrate/20140108105456_create_products.rb:5

错误消息告诉您错误:

An error has occurred, this and all later migrations canceled:

undefined method `ext'   

“20140108105456_create_products”文件中有“ext”字样。并且,这不是该文件知道的方法。

如果您在问题中发布了迁移文件,我将告诉您如何修复它。

答案 2 :(得分:0)

我有一个类似的错误“undefined method`content'for ...”

不知何故,我的迁移文件包含以下内容:

t.content:text

而不是

t.text:content

看起来男人是对的,可能你有t.ext:description而不是t.text。