Search code examples
ruby-on-railsactiverecordpaper-trail-gem

Paper trail gem makes fields with the name version unusable


I have a table named clients backed by an AR model Client. This is the structure of clients table:

clients
  - id <serial primary key>
  - name <varchar(255)>
  - version <varchar(255)>

I have added versioning to it using the paper trail gem. However, when I update or create the record, the column version does not update. And when I do Client.first.version it gives me nil value even though all my records in the table have a version column that is not null or empty. I suspect this is because of paper trail gem. When I switch my project's branch to a version that does not use the gem, I am able to get non-nil value of version.

Is there any workaround to fix this issue?


Solution

  • Sure, there is. Problem is, version is the default association name in paper_trail, thus calling the association on client rather than fetching it's attribute. You can change your model as:

    class Client < ActiveRecord::Base
      has_paper_trail version: :paper_version, versions: :paper_versions
    end
    
    c = Client.first
    c.version #=> row field
    c.paper_versions #=> previous versions.
    

    Edit: Just found that it is also documented in paper_trail here - https://github.com/airblade/paper_trail#6-extensibility