我試圖圍繞預先存在的MySQL數據庫構建Rails應用程序。我創建了rails項目,正確設置了database.yml文件並創建了一個模型User,以便與db中的我的用戶表相對應。當我運行軌道控制檯然而測試出來,這就是我得到:Rails 3:Model.all => NoMethodError:undefined方法'接受'爲零:NilClass
> 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中已經存在的稱爲用戶的表相對應,其主鍵是一個名爲id的整數字段。
你有一些名爲'visitor'的列嗎? – shingara 2012-02-15 15:50:35
@shingara不,我不知道,爲什麼? – 2012-02-15 16:12:51
,因爲它是一個變量調用'visitor'在堆棧跟蹤中是零。所以可能是問題 – shingara 2012-02-15 16:18:14