I've set papertrail to only record changes containing a whodunnit value/when an admin makes a change by using the below condition in my model:
has_paper_trail if: proc { |model| PaperTrail.request.whodunnit.present? }
However I've noticed there are still a decent amount of records being stored with empty whodunnit values. From having a look at the records, these seem to be mostly 'update' actions all having empty object changes for some reason. I am unsure as to why the value is empty, or how it would get saved at all considering the above condition.
I am getting whodunnit values from warden in my application controller using:
def user_for_paper_trail
request.env['warden']&.user(:admin)&.id
end
Has anyone come across similar behaviour before?
Adding a
source_locationandcommandfield to your papertrail versions table will help you:nilwhodunnit changes.To do this, create a migration to add the
source_locationandcommandfields to your Versions table:I have the following set up in my
papertrail.rb. I am using theJSON serializerbut these changes may work with the normal serializer. The code after theserializerdeclaration is what is of interest to you:Note that in any place where record creation occurs outside of a request, you can manually set the
whodunnitvalue to something specific if you don't want it blank. E.g. in my seed file I do the following:In addition to improving the quality of your Papertrail table (knowing which command triggered a record update), this config should help you identify where phantom
whodunnitsare being saved.