带有SIngle表继承情况的质量分配错误

时间:2013-01-20 13:05:13

标签: ruby-on-rails-3 inheritance

我正在设置联系人模块,基本上我正在设置它以便联系人可以拥有无​​限的联系方式,例如x电话号码,电子邮件,地址等。我已经设置了两个不同的表,其中一个用于联系人基础细节和另一个只包含联系方式,如:

Contact
NAME|DETAILS|COUNTRY ....

ContactDetails
TYPE|LOCATION|DETAILS
enter code here

类型可以是电话,传真,电子邮件和位置可以是“工作”,“官方”,“直接”,详细信息是实际的号码或电子邮件。

我最初设置了一个contact_details模型,后来考虑为每个contact_detail设置不同的模型,例如一个名为phone,fax和email的模型,每个模型都继承自contact_details。

以下是我目前的型号:

class Contact < ActiveRecord::Base
  acts_as_citier
  attr_accessible :about, :name, :type

  has_many :contact_details

  accepts_nested_attributes_for :contact_details

end

class Company < Contact
  attr_accessible :company_name, :description, :timezone, :website, :twenty_four_ops, :type
  acts_as_citier
  before_save :set_parent_attributes
####  
end

# this is the contact etail which corresponds to either a phone, email or fax etc
class ContactDetail < ActiveRecord::Base
  attr_accessible :contact_id, :type, :details, :location

  belongs_to :contact
end

The phone and fax classes

class Phone < ContactDetail

end

class Fax < ContactDetail

end

我使用railscasts上的嵌套表单教程设置了一个表单,但基本上我的表单用于输入一个COMPANY对象,它是Contact类的子对象。联系方式与联系方式相关联,因此我假设如果联系人有联系方式,那么联系方式的公司也应该有联系方式。我的表单设置得相当不错,但是当我提交表单时,我得到Can't mass-assign protected attributes: contact_details_attributes error.

我不太确定这里出了什么问题 - 我已经设置了属于上面代码中的attributes_accessible - 这里缺少什么?

2 个答案:

答案 0 :(得分:1)

你在公司课上试过,accept_nested_attributes吗?或者将contact_details_attributes添加到基类

中的attr_accessible列表中

答案 1 :(得分:1)

type是activerecord中的保留属性。您仍然可以通过设置

来使用它
self.inheritance_column = :kind # or provide another name