我有一个这样简单的陈述:
@employee.update_attributes(:subscribed=>false)
但这并未更新boolean
列字段subscribed
。它抛出警告说:
WARNING: Can't mass-assign these protected attributes: subscribed
答案 0 :(得分:3)
我建议使用#update_attribute,而不是#update_attributes。 #update_attribute(singular)接受两个参数:属性名称和值。这用于翻转布尔值或更新单个值。 #update_attribute的语义也意味着不会触发回调。
从您的代码中,这是一个简单的更改:
@employee.update_attribute(:subscribed, false)
现在,您的代码失败的真正原因是您在Employee模型中使用#attr_accessible或#attr_protected的地方。使用#attr_accessible有助于防止注入攻击,只允许从#attributes =(#update_attributes最终调用的内容)中分配某些字段。警告来自#attributes =。
答案 1 :(得分:1)
需要attr_accessible :subscribed
> _<