在Rails 4中,我有一个像这样的has_many关系:
class Company::Appointment < ActiveRecord::Base
has_many :company_appointment_extras, class_name: "Company::Appointment::Extra"
accepts_nested_attributes_for :company_appointment_extras
end
class Company::Appointment::Extra < ActiveRecord::Base
belongs_to :company_appointment, class_name: "Company::Appointment"
end
用户使用嵌套表单同时创建这些对象。一切正常,直到写入数据库,此时我收到此错误:
can't write unknown attribute `appointment_id'
起初我认为错误是从Company::Appointment::Extra
引发的,但当我尝试将foreign_key: "company_appointment_id"
添加到belongs_to
减速时,我继续得到unknown attribute 'appointment_id'
错误,这意味着错误不是来自那种关系。
事实上,在控制台中查看,在编写Company::Appointment
本身时,似乎确实发生了错误:
SQL (1.0ms) INSERT INTO "company_appointments" ("company_id", "created_at", "date", "total", "updated_at", "user_property_id") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id" [["company_id", 1], ["created_at", Thu, 05 Sep 2013 04:58:19 UTC +00:00], ["date", Fri, 06 Sep 2013 00:00:00 UTC +00:00], ["total", #<BigDecimal:6ac2118,'0.55E2',9(27)>], ["updated_at", Thu, 05 Sep 2013 04:58:19 UTC +00:00], ["user_property_id", 1]]
(0.3ms) ROLLBACK
Completed 500 Internal Server Error in 452ms
知道为什么会这样吗?
编辑:
我应该澄清一下,在我的迁移中,我宣布:t.integer :company_appointment_id, null: false
。密钥实际上是company_appointment_id
,而不是appointment_id
。