Today I Learned

hashrocket A Hashrocket project

Get Whodunnit Instance from PaperTrailGlobalId

I ran into an issue trying to get an instance of the User who made a change captured by paper_trail with the paper_trail-globalid gem.

With regular paper_trail, calling whodunnit will give you the id or name of the user who... dunnit.

post.versions.last.whodunnit
# => "Tony Yunker"

With the paper_trail-globalid gem, whodunnit is saved as, well, a global id.

post.versions.last.whodunnit
# => "gid://app/User/12345"

This isn't super helpful on it's own, but actor will resolve the global id and return an instance of the model it references.

post.versions.last.whodunnit
# => <User:0x000000015d618010...>
See More #rails TILs
Looking for help? Hashrocket has been an industry leader in Ruby on Rails since 2008. Rails is a core skill for each developer at Hashrocket, and we'd love to take a look at your project. Contact us and find out how we can help you.