Paper trail object changes and rails enums

149 Views Asked by At

I'm working on versioning models in an old project. almost all of the enum's values used in the project are integers. when tracking changes with object_changes the values are displayed as integers, which is not clear to the end user viewing this data.

for example

class Article  
    has_paper_trail
   enum status: {draft: 0, published: 1}
end

the record is saved with object changes status :[0,1] is there a way either to save or display the status key instead of the value?

1

There are 1 best solutions below

0
Poonam Shah On

You can obtain the readable form of the enum value by using the following code.

version = Article.first.versions.last
version.changeset["status"]

To go even further, migrate your data and define your enums for 'status' as follows.

enum status: {draft: :draft, published: :published}

Rails will save the values as strings; therefore, its attribute must have a compatible data type.