ActiveRecord doesn't work on one table

不想你离开。 提交于 2019-12-11 12:32:23

问题


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


回答1:


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




回答2:


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


来源:https://stackoverflow.com/questions/3651954/activerecord-doesnt-work-on-one-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!