[英]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.