突然间,我的一个模特开始出现两个不同的错误。我不知道我改变了什么,但我的代码似乎不是问题。
我进入了rails控制台。我输入了QuestiNon.find(:all),我得到了迄今为止输入的所有记录。但是,如果我尝试
ruby-1.8.7-p352 :003 > Question.find(55)
NoMethodError: undefined method `eq' for nil:NilClass
from /home/mari/.rvm/gems/ruby-1.8.7-p352/gems/activesupport-3.0.10/lib/active_support/whiny_nil.rb:48:in `method_missing'
from /home/mari/.rvm/gems/ruby-1.8.7-p352/gems/activerecord-3.0.10/lib/active_record/relation/finder_methods.rb:299:in `find_one'
from /home/mari/.rvm/gems/ruby-1.8.7-p352/gems/activerecord-3.0.10/lib/active_record/relation/finder_methods.rb:289:in `find_with_ids'
from /home/mari/.rvm/gems/ruby-1.8.7-p352/gems/activerecord-3.0.10/lib/active_record/relation/finder_methods.rb:107:in `find'
from /home/mari/.rvm/gems/ruby-1.8.7-p352/gems/activerecord-3.0.10/lib/active_record/base.rb:444:in `__send__'
from /home/mari/.rvm/gems/ruby-1.8.7-p352/gems/activerecord-3.0.10/lib/active_record/base.rb:444:in `find'
from (irb):3
我已经仔细检查了,并且id 55确实存在
如果我尝试创建方法,我会得到:
ruby-1.8.7-p352 :004 > Question.create("question" => "something")
NoMethodError: undefined method `name' for nil:NilClass
from /home/mari/.rvm/gems/ruby-1.8.7-p352/gems/activesupport-3.0.10/lib/active_support/whiny_nil.rb:48:in `method_missing'
from /home/mari/.rvm/gems/ruby-1.8.7-p352/gems/arel-2.0.10/lib/arel/visitors/to_sql.rb:56:in `visit_Arel_Nodes_InsertStatement'
from /home/mari/.rvm/gems/ruby-1.8.7-p352/gems/arel-2.0.10/lib/arel/visitors/to_sql.rb:55:in `map'
from /home/mari/.rvm/gems/ruby-1.8.7-p352/gems/arel-2.0.10/lib/arel/visitors/to_sql.rb:55:in `visit_Arel_Nodes_InsertStatement'
from /home/mari/.rvm/gems/ruby-1.8.7-p352/gems/arel-2.0.10/lib/arel/visitors/visitor.rb:15:in `send'
from /home/mari/.rvm/gems/ruby-1.8.7-p352/gems/arel-2.0.10/lib/arel/visitors/visitor.rb:15:in `visit'
from /home/mari/.rvm/gems/ruby-1.8.7-p352/gems/arel-2.0.10/lib/arel/visitors/visitor.rb:5:in `accept'
from /home/mari/.rvm/gems/ruby-1.8.7-p352/gems/arel-2.0.10/lib/arel/visitors/to_sql.rb:18:in `accept'
from /home/mari/.rvm/gems/ruby-1.8.7-p352/gems/activerecord-3.0.10/lib/active_record/connection_adapters/abstract/connection_pool.rb:111:in `with_connection'
from /home/mari/.rvm/gems/ruby-1.8.7-p352/gems/arel-2.0.10/lib/arel/visitors/to_sql.rb:16:in `accept'
在一些人周围搜索表明问题是由于primary_key配置,但我没有设置新的主键。我的模型主键应该是id。
我的问题.rb是这样的:
class Question<的ActiveRecord ::基
set_table_name“questions”
端
我的桌子是这样的:
from (irb):4ruby-1.8.7-p352 :005 > ActiveRecord::Base.connection.columns("questions").map{|c| [c.name, c.type] }
=> [["id", :integer], ["question_type", :integer], ["question", :string], ["mandatory", :boolean], ["help", :string], ["answers", :string], ["question_number", :integer], ["survey_id", :string], ["created_at", :datetime], ["updated_at", :datetime]]
奇怪的是,这在我的服务器上使用MYSQL工作正常,但在本地使用SQLITE3却没有。
我该如何解决这个问题?有任何想法吗?感谢。