开发者

ActiveRecord doesn't work on one table

I have a Rails M开发者_运维技巧odel:

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 which works just fine. I dont know how to catch this error :( Are there any reserved words in the columns or do Rails try to apply some configuration over convention magic on this?

I use Rails3 with Ruby 1.9.2.


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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜