我想使用 after_save 回调将 Updated_by 列设置为 current_user。但 current_user 在模型中不可用。我该怎么做?
您需要在控制器中处理它。首先对模型执行保存,如果成功则更新记录字段。
Example
class MyController < ActionController::Base
def index
if record.save
record.update_attribute :updated_by, current_user.id
end
end
end
另一种选择(我更喜欢这个)是在模型中创建一个包装逻辑的自定义方法。例如
class Record < ActiveRecord::Base
def save_by(user)
self.updated_by = user.id
self.save
end
end
class MyController < ActionController::Base
def index
...
record.save_by(current_user)
end
end
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)