【问题标题】:Mass Assignment error with SIngle Table inheritance situation单表继承情况的批量分配错误
【发布时间】: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


    【解决方案1】:

    您是否尝试过,在公司类中接受_nested_attributes?或者将 contact_details_attributes 添加到基类中的 attr_accessible 列表中

    【讨论】:

      【解决方案2】:

      type 是 activerecord 中的保留属性。你仍然可以通过设置来使用它

      self.inheritance_column = :kind # or provide another name
      

      【讨论】:

      • 我知道并且我试图利用它基本上 Phone 继承自联系人详细信息基类
      猜你喜欢
      • 2016-11-21
      • 2012-12-18
      • 2018-01-13
      • 1970-01-01
      • 2012-08-17
      • 1970-01-01
      • 2014-09-06
      • 1970-01-01
      • 2013-05-11
      相关资源
      最近更新 更多