【问题标题】:trying to use ActiveRecord with Sinatra, Migration fails question尝试将 ActiveRecord 与 Sinatra 一起使用,迁移失败问题
【发布时间】:2011-02-25 19:36:09
【问题描述】:

运行 Sinatra 1.0,我想在我的程序中添加一个数据库表。在我的 Rakefile 中,我有一个任务

task :environment do   
    ActiveRecord::Base.establish_connection(YAML::load(File.open('config/database.yml'))["development"])      
end

我的命名空间中有一个调用迁移代码的迁移任务:

    namespace :related_products do  
      desc "run any migrations we may have in db/migrate"
      task :migrate => :environment do   
        ActiveRecord::Migrator.migrate('db/migrate', ENV["VERSION"] ? ENV["VERSION"].to_i : nil )   
    end 

当调用 ActiveRecord::MIgrator.migrate() 时,我的控制台出现错误。 耙中止! nil:NilClass 的未定义方法“信息”

迁移代码本身非常简单......并且没有让我知道这个缺失的信息类是什么。

    class CreateStores < ActiveRecord::Migration
      def self.up       
        create_table :stores do |t|
          t.string :name
          t.string :access_url
          t.timestamps
        end
      end

      def self.down
        drop_table :stores
      end
    end  

我在这里有点迷惑,正在寻找一些线索来找出可能出了什么问题。谢谢!

一个简单的回溯显示进攻线只是对迁移的调用:

    rake aborted!
undefined method `info' for nil:NilClass
/Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/migration.rb:473:in `migrate'
/Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/migration.rb:472:in `each'
/Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/migration.rb:472:in `migrate'
/Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/migration.rb:400:in `up'
/Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/migration.rb:383:in `migrate'
/Users/baz/Documents/workspace/rp/Rakefile:26
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:636:in `call'
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:636:in `execute'
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:631:in `each'
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:631:in `execute'
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:597:in `invoke_with_call_chain'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/monitor.rb:242:in `synchronize'
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:590:in `invoke_with_call_chain'
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:583:in `invoke'
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:2051:in `invoke_task'
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:2029:in `top_level'
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:2029:in `each'
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:2029:in `top_level'
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:2068:in `standard_exception_handling'
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:2023:in `top_level'
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:2001:in `run'
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:2068:in `standard_exception_handling'
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:1998:in `run'
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/bin/rake:31
/usr/bin/rake:19:in `load'
/usr/bin/rake:19

【问题讨论】:

  • 我们能看到回溯吗?相关行似乎不在提供的代码中...
  • 我添加了我得到的回溯。第 26 行只是调用 ActiveRecord::Migrator.migrate

标签: ruby activerecord sinatra


【解决方案1】:

我刚刚遇到了同样的问题,解决方法是为 ActiveRecord 提供一些关于在何处输出日志信息的信息。

我在我的 Rakefile 中添加了以下内容

ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Migration.verbose = true

-蒂米

【讨论】:

  • 我遇到了一些确切的问题(Heroku/Sinatra/AR),这个解决方案效果很好。谢谢!
猜你喜欢
  • 2020-05-24
  • 1970-01-01
  • 2017-01-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-14
  • 1970-01-01
相关资源
最近更新 更多