我正在尝试围绕预先存在的MySQL数据库构建Rails应用程序。我创建了rails项目,正确设置了database.yml文件并创建了一个模型User,以对应db中的users表。当我运行rails控制台测试它时,这就是我得到的:
> User.all
(32.4ms) SHOW TABLES
(34.9ms) describe `users`
NoMethodError: undefined method `accept' for nil:NilClass
from /home/richard/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.1/lib/active_record/connection_adapters/abstract/database_statements.rb:7:in `to_sql'
from /home/richard/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.1/lib/active_record/connection_adapters/abstract/database_statements.rb:16:in `select_all'
from /home/richard/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.1/lib/active_record/connection_adapters/abstract/query_cache.rb:63:in `select_all'
from /home/richard/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.1/lib/active_record/querying.rb:38:in `block in find_by_sql'
from /home/richard/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.1/lib/active_record/explain.rb:40:in `logging_query_plan'
from /home/richard/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.1/lib/active_record/querying.rb:37:in `find_by_sql'
from /home/richard/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.1/lib/active_record/relation.rb:170:in `exec_queries'
from /home/richard/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.1/lib/active_record/relation.rb:159:in `block in to_a'
from /home/richard/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.1/lib/active_record/explain.rb:33:in `logging_query_plan'
from /home/richard/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.1/lib/active_record/relation.rb:158:in `to_a'
from /home/richard/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.1/lib/active_record/relation/finder_methods.rb:159:in `all'
from /home/richard/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.1/lib/active_record/querying.rb:5:in `all'
from (irb):1
from /home/richard/.rvm/gems/ruby-1.9.3-p0/gems/railties-3.2.1/lib/rails/commands/console.rb:47:in `start'
from /home/richard/.rvm/gems/ruby-1.9.3-p0/gems/railties-3.2.1/lib/rails/commands/console.rb:8:in `start'
from /home/richard/.rvm/gems/ruby-1.9.3-p0/gems/railties-3.2.1/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'1.9.3-p0 :002 > ^C
知道为什么会这样吗?据我所知,我遵循rails约定:我创建了一个User模型,与db中已经存在的名为users的表对应,其主键是一个名为id的整数字段。
答案 0 :(得分:4)
刚刚遇到同样的问题。将其缩小为与MySQL相关的问题,因为它仅在登台环境(MySQL)中发生,而不是在开发环境(sqlite3)中发生。通过将mysql2
从版本0.2.7升级到0.3.11来修复它。
答案 1 :(得分:4)
升级到rails 3.2.2后,我遇到了与MySQL类似的问题。
这就是我所做的修复它。
rvm gem install mysql2
In GemFile remove old MySQL gem and add - gem ‘mysql2'
bundle install
bundle update activerecord-mysql2-adapter
bundle update mysql2
然后重新启动rails服务器。