I have a Customer, Item and Order model. Customer has_many Items through Orders and has_many Orders. Item has_many Customers through Orders and has_many Orders. Order belongs to Customer and Item. I get the ActiveRecord::UnknownAttributeError: unknown attribute 'customer_id' for Order. error when trying to save through the console:
Customer model:
class Customer < ActiveRecord::Base
has_many :orders
has_many :items, through: :orders
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
end
Item model:
class Item < ActiveRecord::Base
has_many :orders
has_many :customers, through: :orders
end
Order model:
class Order < ActiveRecord::Base
belongs_to :item
belongs_to :customer
end
Orders table:
class CreateOrders < ActiveRecord::Migration
def change
create_table :orders, id: false do |t|
t.belongs_to :customers, index: true
t.belongs_to :items, index: true
t.timestamps null: false
end
end
end
Console Command to save Order (note that cuban_sandwich and chris are saved as a new Customer and Item already.)
order1 = chris.orders.build(customers_id: chris.id, items_id: cuban_sandwich.id)
Could I be saving this incorrectly? Or is there a problem with my model/table associations?