Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error reify with has_one: true #29

Open
taufek opened this issue Sep 21, 2021 · 4 comments
Open

Error reify with has_one: true #29

taufek opened this issue Sep 21, 2021 · 4 comments

Comments

@taufek
Copy link

taufek commented Sep 21, 2021

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';
@westonganger
Copy link
Owner

Going to need more details to debug this.

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

@taufek
Copy link
Author

taufek commented Sep 22, 2021

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

@jimmytang
Copy link

jimmytang commented May 17, 2022

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

@wkrsz
Copy link

wkrsz commented Jan 25, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants