【发布时间】:2013-01-20 13:05:12
【问题描述】:
我正在设置一个联系人模块,基本上是在设置它,以便联系人可以拥有无限的联系方式,即 x 电话号码、电子邮件、地址等。我设置了两个不同的表,其中一个用于联系人库详细信息,另一个仅包含联系方式,例如:
Contact
NAME|DETAILS|COUNTRY ....
ContactDetails
TYPE|LOCATION|DETAILS
enter code here
类型可以是电话、传真、电子邮件,位置可以是“工作”、“官方”、“直接”,详细信息是实际号码或电子邮件。
我最初设置了一个contact_details 模型,后来考虑为每个contact_detail 设置不同的模型,例如称为电话、传真和电子邮件的模型,并且每个模型都将从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,因为它们应该在上面的代码中 - 这里缺少什么?
【问题讨论】:
标签: ruby-on-rails-3 inheritance