2

models/order_item.rb

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

models/order.rb

 class Order < ActiveRecord::Base   
     has_many :order_item
 end

Now, I test those by using rails console. When I typed OrderItem, it threw

NameError: uninitialized constant OrderItem

Update

I did this in 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>'
rj487
  • 4,476
  • 6
  • 47
  • 88

2 Answers2

4

NameError: uninitialized constant OrderItem

The association name for has_many should be plural, so change has_many :order_item to has_many :order_items in order.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 i.e., (order_item .rb) in the filename(app/models/order_item .rb). Change it to order_item.rb

Pavan
  • 33,316
  • 7
  • 50
  • 76
1

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.

--

Use Pavan's updated answer; the above should give you an idea as to the type of code you could expect from your models.

Richard Peck
  • 76,116
  • 9
  • 93
  • 147
  • Now I just start create one to many structure, yeah I think I should modify it and use `has_many: through`. Thanks. – rj487 Sep 28 '15 at 09:39