简体   繁体   English

Rails 3 NoMethodError:nil:NilClass的未定义方法“名称”

[英]Rails 3 NoMethodError: undefined method `name' for nil:NilClass

Whenever i tried to use 每当我尝试使用

Table.create :a=>"a", :b=>"b"

or 要么

tab=Table.new
tab.a="a"
tab.b="b"
tab.save!

produces 产生

NoMethodError: undefined method `name' for nil:NilClass NoMethodError:nil:NilClass的未定义方法“名称”

my table model is 我的桌子型号是

class Table < ActiveRecord::Base  
 set_table_name "table"
 set_primary_key "id"
end

As everyone has stated, TABLE is a reserved word. 正如每个人都说过的, TABLE是保留字。 For future reference, if you really wanted to use these reserved words, in your sql queries(I know you are using the AR abstraction), you would have to use backticks like so: 为了将来参考,如果您真的想在SQL查询中使用这些保留字(我知道您正在使用AR抽象),则必须使用反引号,如下所示:

SELECT * FROM `TABLE`

However I would highly suggest changing your table name. 但是,我强烈建议您更改表名。 One other thing to note is that you do not have to set your primary key to id because rails by default uses id for the PK. 需要注意的另一件事是您不必将主键设置为id因为rails默认情况下使用PK的id

TABLE is reserved word in MySQL. TABLE是MySQL中的保留字。

For more reserved words checkout: 如需更多保留字结帐:

http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Rails-NoMethodError-未定义的方法“名称”,为nil:NilClass - Rails - NoMethodError - undefined method `name' for nil:NilClass Rails 4-rake db:seed NoMethodError:nil:NilClass的未定义方法“名称” - Rails 4 - rake db:seed NoMethodError: undefined method `name' for nil:NilClass Rails 4:NoMethodError:nil的未定义方法`each&#39;:NilClass - Rails 4: NoMethodError: undefined method `each' for nil:NilClass Rails DateTime-NoMethodError(nil:NilClass的未定义方法“ []”) - Rails DateTime - NoMethodError (undefined method `[]' for nil:NilClass) Ruby On Rails - NoMethodError:nil:NilClass 的未定义方法“[]” - Ruby On Rails - NoMethodError: undefined method `[]' for nil:NilClass Heroku + Rails:NoMethodError(nil:NilClass的未定义方法“ /”) - Heroku + Rails: NoMethodError (undefined method `/' for nil:NilClass) Ruby on Rails NoMethodError:nil:NilClass的未定义方法“ []” - Ruby on Rails NoMethodError: undefined method `[]' for nil:NilClass Rails - NoMethodError未定义的方法`&gt; =&#39;对于nil:NilClass - Rails - NoMethodError undefined method `>=' for nil:NilClass nil:NilClass Ruby on Rails的未定义方法&#39;&gt;&#39;(NoMethodError) - undefined method `>' for nil:NilClass Ruby on Rails (NoMethodError) nil 的未定义方法“%”:NilClass (NoMethodError) Ruby on Rails - Undefined method `%' for nil:NilClass (NoMethodError) Ruby on Rails
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM