westonganger/paper_trail-association_tracking

Error reify with `has_one: true`

Opened this issue · 4 comments

Whenever I call reify with has_one: true, I'm getting below error:

ActiveRecord::ConfigurationError: Can't join 'PaperTrail::Version' to association named 'version_associations';

Going to need more details to debug this.

What paper trail version are you using?
Which Rails version?
Any other code or details?

I'm using paper_trail outside of Rails. I'm using grape framework.

grape version: 1.1.0
activerecord: 5.2.1
paper_trail version: 10.0.1
paper_trail-association_tracking version: 2.1.3

Below are the migrations:

class CreateVersion < ActiveRecord::Migration[5.1]
  TEXT_BYTES = 1_073_741_823

  def change
    create_table :versions do |t|
      t.string   :item_type
      t.integer  :item_id,   null: false
      t.string   :event,     null: false
      t.string   :whodunnit
      t.text     :object, limit: TEXT_BYTES
      t.datetime :created_at
    end
    add_index :versions, %i(item_type item_id)
  end
end

class CreateVersionAssociations < ActiveRecord::Migration[5.2]
  def up
    create_table :version_associations do |t|
      t.integer  :version_id
      t.string   :foreign_key_name, null: false
      t.integer  :foreign_key_id
      t.string   :foreign_type
    end
    add_index :version_associations, [:version_id]
    add_index :version_associations,
      %i(foreign_key_name foreign_key_id foreign_type),
      name: "index_version_associations_on_foreign_key"
  end

  def down
    remove_index :version_associations, [:version_id]
    remove_index :version_associations,
      name: "index_version_associations_on_foreign_key"
    drop_table :version_associations
  end
end

class CreateProducts < ActiveRecord::Migration[5.1]
  def change
    create_table :products, id: false do |t|
      t.string    :uuid, limit: 36, null: false
      t.string    :name
    end
  end
end

class CreateUsers < ActiveRecord::Migration[5.1]
  def change
    create_table :users, id: false do |t|
      t.string    :uuid, limit: 36, null: false
      t.string    :name
      t.string :product_id, null: false, index: true, foreign_key: true
    end
  end
end

Below are the active records:

class Product < ActiveRecord::Base
   has_one :user
   has_paper_trail
end

class User < ActiveRecord::Base
   belongs_to :product
   has_paper_trail
end

Below is the initializer for paper_trail:

require "paper_trail_association_tracking/frameworks/active_record"

PaperTrail.config.track_associations = true

I'm also seeing this. Did you find a solution? Appreciate any pointers 🙏

It's probably due to Railtie not loading:

      ::PaperTrail::Version.include(::PaperTrailAssociationTracking::VersionConcern)

https://github.com/westonganger/paper_trail-association_tracking/blob/master/lib/paper_trail_association_tracking/frameworks/rails/railtie.rb#L13

I opted to manually add the associatio:

class WidgetVersion < ActiveRecord::Base
  include PaperTrail::VersionConcern

  has_many :version_associations, class_name: 'PaperTrail::VersionAssociation', foreign_key: :version_id, dependent: :destroy, inverse_of: :widget_version