我在rails中有以下模型(简化):
class Phone < ActiveRecord::Base
include ActiveModel::Validations
belongs_to :user
belongs_to :brand
attr_readonly :user, :brand
attr_accessible :model, :phone_number
validates :user, :presence => true
validates :brand, :presence => true
validates :model, :presence => true
validates :phone_number, :presence => true
end
根据文档, attr_readonly 应该允许在创建时设置属性,但不能在更新时设置。
但是,当我这样做时:
Phone.create(:user => <existing_user>, :brand => <existing_brand>, :model => "Galaxy", :phone_number => "555-433-5678")
我收到此错误:
Can't mass-assign protected attributes user, brand
我错过了什么?