【发布时间】:2016-02-25 10:10:18
【问题描述】:
我有一个 Customer、Item 和 Order 模型。 Customer has_many Items 通过 Orders 和 has_many Orders。项目 has_many Customers through Orders 和 has_many Orders。订单属于客户和项目。尝试通过控制台保存时出现ActiveRecord::UnknownAttributeError: unknown attribute 'customer_id' for Order. 错误:
客户模型:
class Customer < ActiveRecord::Base
has_many :orders
has_many :items, through: :orders
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
end
物品型号:
class Item < ActiveRecord::Base
has_many :orders
has_many :customers, through: :orders
end
订购模型:
class Order < ActiveRecord::Base
belongs_to :item
belongs_to :customer
end
订单表:
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
保存订单的控制台命令 (注意 cuban_sandwich 和 chris 已经被保存为一个新的 Customer 和 Item。)
order1 = chris.orders.build(customers_id: chris.id, items_id: cuban_sandwich.id)
我会错误地保存这个吗?还是我的模型/表格关联有问题?
【问题讨论】:
标签: ruby-on-rails activerecord