westonganger / paper_trail-association_tracking

Plugin for the PaperTrail gem to track and reify associations
MIT License
128 stars 38 forks source link

FIX: klass not work for active_hash #44

Closed smptmhr closed 3 months ago

smptmhr commented 1 year ago

problem

My code has Product and ProductMaster classes.

def Product < ApplicationRecord
  has_paper_trail
  belongs_to_active_hash :product_master, class_name: ProductMaster
end
class ProductMaster < ActiveHash::Base
  include ActiveHash::Associations

  self.data = [
    {
      id: 1,
      name: 'product_a',
      rank: 1000,
    },
    {
      id: 2,
      name: 'product_b',
      rank: 2000,
    },
    {
      id: 3,
      name: 'product_c',
      rank: 3000,
    },   
  ]

  has_many :products
end

When updating a Product instance, updating failed with following error messages.

Rails couldn't find a valid model for ProductMaster association.
Please provide the :class_name option on the association declaration.
If :class_name is already provided, make sure it's an ActiveRecord::Base subclass.

I saw record_update method in lib/paper_trail_association_tracking/record_trail.rb and debug the method. Finally,I found the error is occurring here.

https://github.com/westonganger/paper_trail-association_tracking/blob/c8e8b2cc06f25d8563338ee08a95e6d9e2e335bf/lib/paper_trail_association_tracking/record_trail.rb#L177

assoc cannot return class name because klass is not defined in ActiveHash, I guess.

solution suggesting

In my local environment, using constantize works for updating a model which have associations defined by both belongs_to_active_hash and belongs_to(active_record).

elsif ::PaperTrail.request.enabled_for_model?(assocclass.name.constantize)
westonganger commented 3 months ago

Closing due to inactivity