简体   繁体   中英

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

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. 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:

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.

TABLE is reserved word in MySQL.

For more reserved words checkout:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM