简体   繁体   中英

NameError: uninitialized constant Item

I'm currently working on a Ruby on Rails app. I'm trying to create some Objects of type Item, but it cant find my model, which is already defined. What am i doing wrong?

snippet:

parse.rake (app/lib/tasks):

item = Item.create!(id: item_array['id'], path: item_array['path'], type: item_array['type'], lang: item_array['lang'], component: item_array['component'],
            content: item_array['content'], title: item_array['title'], index: item_array['index'], tags: item_array['tags'], created_at: item_array['created_at'],
            updated_at: item_array['updated_at'], keyword: item_array['keyword'], description: item_array['description']);

item.rb (app/models)

class Item < ActiveRecord::Base
  self.table_name = 'my_items'
  self.primary_key = 'id'
end

schema.rb (app/db):

ActiveRecord::Schema.define(version: 20160623145822) do


 create_table 'items', force: :cascade, options: 'ENGINE=InnoDB DEFAULT CHARSET=utf8' do |t|
    t.string   'path'
    t.string   'type'
    t.string   'lang'
    t.string   'component'
    t.json     'content'
    t.string   'title'
    t.integer  'index'
    t.json     'tags'
    t.string   'keyword'
    t.text     'description', limit: 65535
    t.datetime 'created_at',                null: false
    t.datetime 'updated_at',                null: false
  end

end

In class Item you set a table 'my_items', but in the scheme, you are creating table 'items'.

I think you can delete table_name and change primary_key in model Item. Because RoR by default will use table 'items' and primary key 'id'.

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