我有一篇模型文章。一旦创建了它,我们就向用户发送一个带有令牌的激活代码。此激活代码是故意黑名单的,因此没有添加到attr_accesible列表中。
一旦用户回来激活代码
articleitem = Article.find_by_activation_code(params[:code])
articleitem.activation_code = ""现在我们如何更新记录。我不想使用保存,因为它激活了before_save方法
我已经在控制器上做了以下所有的尝试。
articleItem.update(activation_code: "")
update method is private
articleItem.update_attributes(activation_code: "")
WARNING: Can't mass-assign protected attributes: activation_code更新记录的其他替代方法有哪些?
发布于 2014-08-25 07:00:52
另一种方法是设置虚拟属性(未经测试的代码)。
in model
attr_accessor :execute_before_save
before_save :some_method
def some_method
# Check is execute_before_save is set, if not let the method execute no matter what
if execute_before_save || true
# your code follows
end
end
in your controller
articleitem = Article.find_by_activation_code(params[:code])
articleItem.execute_before_save = false
articleitem.activation_code = ""
articleitem.save这样,您就可以控制before_save回调,但它要求您设置虚拟属性,这是开销。
如果有帮助请告诉我。
https://stackoverflow.com/questions/25477950
复制相似问题