2012-02-15 67 views
5

我試圖圍繞預先存在的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的整數字段。

+0

你有一些名爲'visitor'的列嗎? – shingara 2012-02-15 15:50:35

+0

@shingara不,我不知道,爲什麼? – 2012-02-15 16:12:51

+0

,因爲它是一個變量調用'visitor'在堆棧跟蹤中是零。所以可能是問題 – shingara 2012-02-15 16:18:14

回答

4

剛剛有同樣的問題。由於它僅在分段環境(MySQL)中發生,而不是在開發環境(sqlite3)中發生,因此將其縮小爲與MySQL相關的問題。通過將mysql2從版本0.2.7升級到0.3.11來修復它。

+0

這對我有用。編輯好的Gemfile刪除版本'<0.3'。然後調用'bundle update mysql2'並用'rake db:drop'重新創建數據庫,後跟'rake --trace db:setup'。 – peterept 2012-03-07 21:56:39

+0

我在非Rails應用程序中使用0.3.11,並且仍然存在此問題。 – 2013-02-26 22:06:30

+1

@BenHamill,我也有這個問題0.3.11,非Rails。通過從我的gemspec中刪除activerecord-mysql2-adapter來解決它。 – 2013-07-10 21:18:32

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服務器。

相關問題