升级到Rails 3后,未定义的方法'accept'为nil:NilClass

时间:2012-08-09 18:25:53

标签: ruby-on-rails ruby-on-rails-3.2 mysql-5.1

我刚刚将Rails应用程序从Rails 2.3.8升级到Rails 3.2.7,我遇到了Activerecord的问题。

如果我尝试使用Activerecord访问我的数据库(例如:Employee.last),我会收到以下错误:

NoMethodError: undefined method `accept' for nil:NilClass
from /home/aneyer/.rvm/gems/ruby-1.8.7-p370@rails3/gems/activerecord-3.2.7/lib/active_record/connection_adapters/abstract/database_statements.rb:7:in `to_sql'
from /home/aneyer/.rvm/gems/ruby-1.8.7-p370@rails3/gems/activerecord-3.2.7/lib/active_record/connection_adapters/abstract/database_statements.rb:18:in `select_all'
from /home/aneyer/.rvm/gems/ruby-1.8.7-p370@rails3/gems/activerecord-3.2.7/lib/active_record/connection_adapters/abstract/query_cache.rb:63:in `select_all'
from /home/aneyer/.rvm/gems/ruby-1.8.7-p370@rails3/gems/activerecord-3.2.7/lib/active_record/querying.rb:38:in `find_by_sql'
from /home/aneyer/.rvm/gems/ruby-1.8.7-p370@rails3/gems/activerecord-3.2.7/lib/active_record/explain.rb:40:in `logging_query_plan'
from /home/aneyer/.rvm/gems/ruby-1.8.7-p370@rails3/gems/activerecord-3.2.7/lib/active_record/querying.rb:37:in `find_by_sql'
from /home/aneyer/.rvm/gems/ruby-1.8.7-p370@rails3/gems/activerecord-3.2.7/lib/active_record/relation.rb:171:in `exec_queries'
from /home/aneyer/.rvm/gems/ruby-1.8.7-p370@rails3/gems/activerecord-3.2.7/lib/active_record/relation.rb:160:in `to_a'
from /home/aneyer/.rvm/gems/ruby-1.8.7-p370@rails3/gems/activerecord-3.2.7/lib/active_record/explain.rb:33:in `logging_query_plan'
from /home/aneyer/.rvm/gems/ruby-1.8.7-p370@rails3/gems/activerecord-3.2.7/lib/active_record/relation.rb:159:in `to_a'
from /home/aneyer/.rvm/gems/ruby-1.8.7-p370@rails3/gems/activerecord-3.2.7/lib/active_record/relation/finder_methods.rb:378:in `find_first'
from /home/aneyer/.rvm/gems/ruby-1.8.7-p370@rails3/gems/activerecord-3.2.7/lib/active_record/relation/finder_methods.rb:122:in `first'
from /home/aneyer/.rvm/gems/ruby-1.8.7-p370@rails3/gems/activerecord-3.2.7/lib/active_record/relation/finder_methods.rb:336:in `find_one'
from /home/aneyer/.rvm/gems/ruby-1.8.7-p370@rails3/gems/activerecord-3.2.7/lib/active_record/relation/finder_methods.rb:312:in `find_with_ids'
from /home/aneyer/.rvm/gems/ruby-1.8.7-p370@rails3/gems/activerecord-3.2.7/lib/active_record/relation/finder_methods.rb:107:in `find'
from /home/aneyer/.rvm/gems/ruby-1.8.7-p370@rails3/gems/activerecord-3.2.7/lib/active_record/querying.rb:5:in `__send__'
from /home/aneyer/.rvm/gems/ruby-1.8.7-p370@rails3/gems/activerecord-3.2.7/lib/active_record/querying.rb:5:in `find'

我在Ruby 1.8.7中使用Rails 3.2.7。我以前使用的是mysql 2.7版本,但在升级时遇到了错误,所以我现在使用的是mysql2(0.311)以及activerecord-myql2-adapter(0.0.3)。

问题似乎与此相似:

Rails 3: Model.all => NoMethodError: undefined method 'accept' for nil:NilClass

但我尝试了那里提出的所有解决方案,但都没有。

编辑: 如果我取出'activerecord-mysql2-adapter'gem,我会收到以下错误。

Address Load (0.7ms)  SELECT `addresses`.* FROM `addresses` LIMIT 1
NoMethodError: undefined method `generated_methods?' for #<Class:0x7f10733eb2e8>
from /home/aneyer/.rvm/gems/ruby-1.8.7-p370@rails3/gems/activerecord-3.2.7/lib/active_record/dynamic_matchers.rb:50:in `method_missing'
from /home/aneyer/.rvm/gems/ruby-1.8.7-p370@rails3/gems/markos_validates_timeliness-2.3.2.2/lib/validates_timeliness/active_record/attribute_methods.rb:46:in `define_attribute_methods'
from /home/aneyer/.rvm/gems/ruby-1.8.7-p370@rails3/gems/activerecord-3.2.7/lib/active_record/attribute_methods.rb:168:in `respond_to?'
from /home/aneyer/.rvm/gems/ruby-1.8.7-p370@rails3/gems/activesupport-3.2.7/lib/active_support/callbacks.rb:398:in `__run_callback'
from /home/aneyer/.rvm/gems/ruby-1.8.7-p370@rails3/gems/activesupport-3.2.7/lib/active_support/callbacks.rb:385:in `_run_find_callbacks'
from /home/aneyer/.rvm/gems/ruby-1.8.7-p370@rails3/gems/activesupport-3.2.7/lib/active_support/callbacks.rb:81:in `send'
from /home/aneyer/.rvm/gems/ruby-1.8.7-p370@rails3/gems/activesupport-3.2.7/lib/active_support/callbacks.rb:81:in `run_callbacks'
from /home/aneyer/.rvm/gems/ruby-1.8.7-p370@rails3/gems/activerecord-3.2.7/lib/active_record/base.rb:523:in `init_with'
from /home/aneyer/.rvm/gems/ruby-1.8.7-p370@rails3/gems/activerecord-3.2.7/lib/active_record/inheritance.rb:68:in `instantiate'
from /home/aneyer/.rvm/gems/ruby-1.8.7-p370@rails3/gems/activerecord-3.2.7/lib/active_record/querying.rb:38:in `find_by_sql'
from /home/aneyer/.rvm/gems/ruby-1.8.7-p370@rails3/gems/activerecord-3.2.7/lib/active_record/querying.rb:38:in `collect!'
from /home/aneyer/.rvm/gems/ruby-1.8.7-p370@rails3/gems/activerecord-3.2.7/lib/active_record/querying.rb:38:in `find_by_sql'
from /home/aneyer/.rvm/gems/ruby-1.8.7-p370@rails3/gems/activerecord-3.2.7/lib/active_record/explain.rb:40:in `logging_query_plan'
from /home/aneyer/.rvm/gems/ruby-1.8.7-p370@rails3/gems/activerecord-3.2.7/lib/active_record/querying.rb:37:in `find_by_sql'
from /home/aneyer/.rvm/gems/ruby-1.8.7-p370@rails3/gems/activerecord-3.2.7/lib/active_record/relation.rb:171:in `exec_queries'
from /home/aneyer/.rvm/gems/ruby-1.8.7-p370@rails3/gems/activerecord-3.2.7/lib/active_record/relation.rb:160:in `to_a'
from /home/aneyer/.rvm/gems/ruby-1.8.7-p370@rails3/gems/activerecord-3.2.7/lib/active_record/explain.rb:33:in `logging_query_plan'
from /home/aneyer/.rvm/gems/ruby-1.8.7-p370@rails3/gems/activerecord-3.2.7/lib/active_record/relation.rb:159:in `to_a'
from /home/aneyer/.rvm/gems/ruby-1.8.7-p370@rails3/gems/activerecord-3.2.7/lib/active_record/relation/finder_methods.rb:378:in `find_first'
from /home/aneyer/.rvm/gems/ruby-1.8.7-p370@rails3/gems/activerecord-3.2.7/lib/active_record/relation/finder_methods.rb:122:in `first'
from /home/aneyer/.rvm/gems/ruby-1.8.7-p370@rails3/gems/activerecord-3.2.7/lib/active_record/querying.rb:5:in `__send__'
from /home/aneyer/.rvm/gems/ruby-1.8.7-p370@rails3/gems/activerecord-3.2.7/lib/active_record/querying.rb:5:in `first'

2 个答案:

答案 0 :(得分:10)

想出来。结果很久以前有人制作的自定义宝石造成了一些错误。删除该宝石修复它。

对于有类似问题的人,请确保删除'activerecord-mysql2-adapter'gem。这就是导致最初问题的原因。

答案 1 :(得分:5)

我在Gemfile中将'activerecord-mysql2-adapter'更改为'mysql2',然后就可以了