ruby-on-railsversion-controlpaper-trail-gem

How do I get the original value of an attribute of a model using the gem PaperTrail?


I have a model that is tracked using PaperTrail.

I would like to compare the current value of the attribute 'name' to the value when it was first created. I can get the current value easily. How do I get its original value?

To get its current value: widget.name

To get its original value: widget.versions.first.???.name

I know that widget.versions.first.changeset will return a hash as follows.

{
    name: [nil, 'original name']
}

Although, I do not want to parse it; there must be a better way to obtain the original value.


Solution

  • The following line of code ended up resolving the issue.

    original = widget.versions.where(event: 'create')[0].changeset['name'][1]

    It's ugly but it works! I can "monkey patch" it into PaperTrail to clean it up.