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

FIX: klass not work for active_hash #44

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

smptmhr
Copy link

@smptmhr smptmhr commented Jul 24, 2023

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.

elsif ::PaperTrail.request.enabled_for_model?(assoc.klass)

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)

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

Successfully merging this pull request may close these issues.

None yet

1 participant