Rail3 attr_accessbile用于嵌套模型

时间:2011-05-26 16:07:08

标签: ruby-on-rails activerecord ruby-on-rails-3.1

我很难理解为什么这些属性仍然受到保护。我应该使用父模型中的范围来允许这些属性吗?

# Email model

class EmailAddress < ActiveRecord::Base
  belongs_to :emailable, :polymorhpic => true

  validates_presence_of :address
  validates_format_of :address, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i

  attr_accessible :address, :location
end

# Company model

class Company < ActiveRecord::Base
  has_many :email_addresses, :as => :emailable, :dependent => :destroy

  validates_presence_of :company_name

  accepts_nested_attributes_for :email_addresses, :reject_if => proc { |attributes| attributes[:address].blank? }

  attr_accessible :company_name, :background, :email_addresses_attributes
end

# Error log

WARNING: Can't mass-assign protected attributes: address, location

0 个答案:

没有答案