Skip to content

Setting whodunnit in the rails console

Jared Beck edited this page Jul 13, 2015 · 22 revisions

In a console session you can manually set who is responsible like this:

PaperTrail.whodunnit = 'Andy Stewart'
widget.update_attributes :name => 'Wibble'
widget.versions.last.whodunnit              # Andy Stewart

You can avoid having to do this manually by setting your initializer to pick up the username of the current user from the OS, like this:

# config/initializers/paper_trail.rb

# the following line is required for PaperTrail >= 4.0.0 with Rails
PaperTrail::Rails::Engine.eager_load!

if defined?(::Rails::Console)
  PaperTrail.whodunnit = "#{`whoami`.strip}: console"
elsif File.basename($0) == "rake"
  PaperTrail.whodunnit = "#{`whoami`.strip}: rake #{ARGV.join ' '}"
end