我的Client
模型attr_accessible
是这样的:
attr_accessible :email, :firm_id, :name, :phone, :last_contact
然后我有这个回调:
after_save :set_score
然后这是set_score
方法(为简洁起见截断):
private
def set_score
score = Score.find_or_create_by_user_id(:user_id => user.id)
client = self
firm = self.firm
days_since_last_contact = (Date.today - client.last_contact).to_i
score.update_attributes(:firm_size => self.firm_size, :days_since_last_contact => days_since_last_contact)
end
这是我得到的错误:
ActiveModel::MassAssignmentSecurity::Error in ClientsController#create
Can't mass-assign protected attributes: days_since_last_contact
app/models/client.rb:124:in `set_score'
app/controllers/clients_controller.rb:67:in `block in create'
app/controllers/clients_controller.rb:66:in `create'
line 124
就是上面的那行score.update_attributes...
。
我没有名为days_since_last_contact
. 那是作为Date
计算结果的局部变量。
关于这里可能发生的事情的想法?
编辑1:
Score
的属性可访问白名单:
attr_accessible :client_id, :days_since_contact....