I'd like to use ActiveRecord's before_save callback to help me track which users edit which records. For example
class Foo < ActiveRecord::Base
before_save
self.edited_by = curent_user.username
end
....
end
The problem I'm running into h开发者_高级运维ere is that Foo has no idea what current_user is because that's coming from the controller helper.
I could do this all in the controller, I realize. But it'd be nice to just drop this into the before_save callback if I could.
Easiest way to do this is to make your model richer:
class Article
def edit!(editor)
self.edited_by = editor.name
self.save!
end
end
No need to use callbacks.
精彩评论