0

在 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

4

0 回答 0