ActiveRecord does not work on one table

I have a Rails model:

ruby-1.9.2-p0 > NavItem => NavItem(id: integer, item_identifier: string, description: string, description2: string, packing_unit: string, sales_unit_of_measure: string, ean_code: string, evp_price: string, item_category_code: string, class: string, product_group_code: string, maintenance_status: string) 

If I want to create a record:

 ruby-1.9.2-p0 > NavItem.create NoMethodError: undefined method `has_key?' for nil:NilClass from /Users/amueller/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.0/lib/active_support/whiny_nil.rb:48:in `method_missing' from /Users/amueller/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.0/lib/active_record/attribute_methods/read.rb:69:in `class' from /Users/amueller/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.0/lib/active_record/persistence.rb:285:in `attributes_from_column_definition' from /Users/amueller/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.0/lib/active_record/locking/optimistic.rb:62:in `attributes_from_column_definition' from /Users/amueller/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.0/lib/active_record/base.rb:1396:in `initialize' from /Users/amueller/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.0/lib/active_record/base.rb:496:in `new' from /Users/amueller/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.0/lib/active_record/base.rb:496:in `create' from (irb):20 from /Users/amueller/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.0/lib/rails/commands/console.rb:44:in `start' from /Users/amueller/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.0/lib/rails/commands/console.rb:8:in `start' from /Users/amueller/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.0/lib/rails/commands.rb:23:in `<top (required)>' from script/rails:6:in `require' from script/rails:6:in `<main>' 

What can I do? I have other models that work great. I don’t know how to catch this error :( Are there any reserved words in the columns or is Rails trying to apply some convention magic configuration on this?

I am using Rails3 with Ruby 1.9.2.

+1
source share
2 answers

You are using class (reserved word) as one of the column names. If you change this, you should be fine.

+5
source

If you do not want to change the database schema, you can override the "class" method:

 class NavItem < ActiveRecord::Base def class NavItem end end 
0
source

All Articles