سؤال

I have a simple Observer that is watching certain models and writing the model to_json to a table along with the model name. I want to also track who performed the change, but I can't figure out how to access the current user.

Perhaps there is a simple gem or bit of code I can put in ApplicationController or something like that that could help?

هل كانت مفيدة؟

المحلول

There's no clean way to do this that I can think of. You could try using the User.current pattern but I'm not a fan of that method and I'm not sure it would communicate to observers properly.

In cases like these where I want to know who (last) changed the object, I usually add a 'changed_by' and 'changed_at' type of attribute to the model itself, and set those in the controller. That would make it much easier to leave an audit trail as well. You could even use attr_accessor if you don't actually want to store the values in the database.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top