本文介绍了活动记录3.2.18中未定义的方法"alias_method_chain",不进行rails迁移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的Rakefile

This is my 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

执行任务时出现此错误:

When I execute the task I got this error:

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'

我进行了一些调试和测试... 然后我移到了Active Record的3.2.13 版本,所有工作均按预期进行.我没有找到3.2.18版本的任何文档...

I had some debug and some test... Then i moved to 3.2.13 version of active record, and all worked as expected. I didn't find any docs for 3.2.18 version...

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

I don't mind to use 3.2.13 version, but I got curious about that.

推荐答案

首先,您使用的是ActiveRecord 2.3.18,而不是3.2.18(据我所知,后者不存在). ActiveRecord 2.3已经过时了,我不认为它与您正在使用的Ruby 2.0兼容.

First of all, you were using ActiveRecord 2.3.18, not 3.2.18 (the latter of which does not exist, to my knowledge). ActiveRecord 2.3 is getting to be quite old, and I don't believe it's compatible with Ruby 2.0, which you're using.

但是,我认为核心问题是Rails 2.2-2.3中的 ,将alias_method_chain移至ActiveSupport模块,然后再将其移至3.0中的Module.您没有在任务中包括ActiveSupport,而我认为这就是导致问题的原因.

But the central issue, I think, is that in Rails 2.2-2.3, alias_method_chain was moved to the ActiveSupport module, before being moved back to Module in 3.0. You're not including ActiveSupport in your task, and I think that's what was causing the problem.

因此,我认为快速解决方案仅是require "active_support".

So I think a quick fix would be just to require "active_support".

这篇关于活动记录3.2.18中未定义的方法"alias_method_chain",不进行rails迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 05:25