简体   繁体   English

Rails:NameError:未初始化的常量OrderItem

[英]Rails: NameError: uninitialized constant OrderItem

models/order_item.rb 车型/ order_item.rb

class OrderItem < ActiveRecord::Base    
  belongs_to :item
  belongs_to :order 
  belongs_to :user
end

models/order.rb 车型/ order.rb

 class Order < ActiveRecord::Base   
     has_many :order_item
 end

Now, I test those by using rails console . 现在,我通过使用rails console测试。 When I typed OrderItem , it threw 当我输入OrderItem ,它抛出了

NameError: uninitialized constant OrderItem

Update 更新

I did this in rails console 我在rails console做到了

2.1.3 :021 >   reload!
Reloading...
 => true 
2.1.3 :022 > Order
 => Order(id: integer, user_id: integer, created_at: datetime, updated_at: datetime, status: integer) 
2.1.3 :023 > Item
 => Item(id: integer, status: integer, name: string, price: integer, descript: text, created_at: datetime, updated_at: datetime, cover_file_name: string, cover_content_type: string, cover_file_size: integer, cover_updated_at: datetime, cate_id: integer) 
2.1.3 :024 > User
 => User(id: integer, email: string, encrypted_password: string, created_at: datetime, updated_at: datetime) 
2.1.3 :025 > OrderItem
NameError: uninitialized constant OrderItem
    from (irb):25
    from /Users/Coda/.rvm/gems/ruby-2.1.3@rails416/gems/railties-4.2.3/lib/rails/commands/console.rb:110:in `start'
    from /Users/Coda/.rvm/gems/ruby-2.1.3@rails416/gems/railties-4.2.3/lib/rails/commands/console.rb:9:in `start'
    from /Users/Coda/.rvm/gems/ruby-2.1.3@rails416/gems/railties-4.2.3/lib/rails/commands/commands_tasks.rb:68:in `console'
    from /Users/Coda/.rvm/gems/ruby-2.1.3@rails416/gems/railties-4.2.3/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
    from /Users/Coda/.rvm/gems/ruby-2.1.3@rails416/gems/railties-4.2.3/lib/rails/commands.rb:17:in `<top (required)>'
    from /Users/Coda/Desktop/code/ruby_pra/shop/bin/rails:8:in `<top (required)>'
    from /Users/Coda/.rvm/rubies/ruby-2.1.3/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    from /Users/Coda/.rvm/rubies/ruby-2.1.3/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    from -e:1:in `<main>'

NameError: uninitialized constant OrderItem NameError:未初始化的常量OrderItem

The association name for has_many should be plural , so change has_many :order_item to has_many :order_items in order.rb 协会名称 has_many应该是plural ,所以改变has_many :order_itemhas_many :order_itemsorder.rb

#order.rb
class Order < ActiveRecord::Base   
  has_many :order_items #plural
end

Update: 更新:

Looking into your code on github , there is a space between order_item and .rb ie, ( order_item .rb ) in the filename ( app/models/order_item .rb ). 展望在github你的代码,有之间的空间 order_item.rb即( order_item .rb文件名 )( app/models/order_item .rb )。 Change it to order_item.rb 将其更改为order_item.rb

In light of the comments, and to remove any doubt, here's what I'd expect from the code : 根据注释,并消除任何疑问,这是我期望从代码中得到的结果

#app/models/order.rb
class Order < ActiveRecord::Base
   belongs_to :user #-> the order belongs to user, so if order_items belong to order, they belong to user, right?
   has_many :order_items
   has_many :items, through: :order_items
end

#app/models/order_item.rb
class OrderItem < ActiveRecord::Base
  #table name "order_items"
  #columns id | item_id | order_id | created_at | updated_at
  belongs_to :item
  belongs_to :order 
end

This is a typical has_many :through relationship, as I'm sure you're aware. 我敢肯定,这是典型的has_many :through关系。

-- -

Use Pavan's updated answer; 使用Pavan更新的答案; the above should give you an idea as to the type of code you could expect from your models. 上面的内容应该使您对模型可以期望的代码类型有所了解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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