0

这是我的 Rakefile

require 'bundler'
Bundler.setup
require 'active_record'
require 'sqlite3'
require 'yaml'
require 'logger'

task :migrate => :environment do
   ActiveRecord::Migrator.migrate('db/migrate', ENV["VERSION"] ? ENV["VERSION"].to_i : nil )
end

task :environment do
  ActiveRecord::Base.establish_connection(YAML::load(File.open('config/database.yaml'))['development'])
  ActiveRecord::Base.logger = Logger.new(STDOUT)
end

当我执行任务时,出现此错误:

rake aborted!
/usr/local/rvm/gems/ruby-2.0.0-p0@ta/gems/activerecord-2.3.18/lib/active_record/base.rb:2449: warning: already initialized constant Class::VALID_FIND_OPTIONS
/usr/local/rvm/gems/ruby-2.0.0-p0@ta/gems/activerecord-2.3.18/lib/active_record/base.rb:2449: warning: previous definition of VALID_FIND_OPTIONS was here
undefined method `alias_method_chain' for #<Class:0x00000001606340>
/usr/local/rvm/gems/ruby-2.0.0-p0@ta/gems/activerecord-2.3.18/lib/active_record/base.rb:2002:in `method_missing'
/usr/local/rvm/gems/ruby-2.0.0-p0@ta/gems/activerecord-2.3.18/lib/active_record/validations.rb:387:in `block in included'
/usr/local/rvm/gems/ruby-2.0.0-p0@ta/gems/activerecord-2.3.18/lib/active_record/validations.rb:386:in `class_eval'
/usr/local/rvm/gems/ruby-2.0.0-p0@ta/gems/activerecord-2.3.18/lib/active_record/validations.rb:386:in `included'
/usr/local/rvm/gems/ruby-2.0.0-p0@ta/gems/activerecord-2.3.18/lib/active_record/base.rb:3210:in `include'
/usr/local/rvm/gems/ruby-2.0.0-p0@ta/gems/activerecord-2.3.18/lib/active_record/base.rb:3210:in `block in <module:ActiveRecord>'
/usr/local/rvm/gems/ruby-2.0.0-p0@ta/gems/activerecord-2.3.18/lib/active_record/base.rb:3208:in `class_eval'
/usr/local/rvm/gems/ruby-2.0.0-p0@ta/gems/activerecord-2.3.18/lib/active_record/base.rb:3208:in `<module:ActiveRecord>'
/usr/local/rvm/gems/ruby-2.0.0-p0@ta/gems/activerecord-2.3.18/lib/active_record/base.rb:5:in `<top (required)>'
/home/marco/desenv/technical_analysis/Rakefile:14:in `block in <top (required)>'
/usr/local/rvm/gems/ruby-2.0.0-p0@ta/gems/rake-10.0.4/lib/rake/task.rb:246:in `call'
/usr/local/rvm/gems/ruby-2.0.0-p0@ta/gems/rake-10.0.4/lib/rake/task.rb:246:in `block in execute'

我进行了一些调试和一些测试......然后我转到了 3.2.13版本的活动记录,一切都按预期工作。我没有找到 3.2.18 版本的任何文档...

我不介意使用 3.2.13 版本,但我对此感到好奇。

4

1 回答 1

3

首先,您使用的是 ActiveRecord 2.3.18,而不是 3.2.18(据我所知,后者不存在)。ActiveRecord 2.3 已经很老了,我不相信它与您正在使用的 Ruby 2.0 兼容。

但我认为,核心问题是在 Rails 2.2-2.3中,在被移回3.0之前被移到alias_method_chain了模块中。你没有包括在你的任务中,我认为这就是导致问题的原因。ActiveSupportModuleActiveSupport

所以我认为快速解决方法就是require "active_support".

于 2013-04-13T15:06:01.123 回答