audited icon indicating copy to clipboard operation
audited copied to clipboard

Undefined class/module error

Open korrs opened this issue 7 years ago • 4 comments

ArgumentError: undefined class/module MyAnyUploader::Uploader70191477934880

This happens when i use audited for versioning model with field which used for CarrierWave. Audited write to audited_changes ruby object(CarrierWave uploader instance).

Error occurs when audited try to load from yaml ruby object and can't find dynamically generated CarrierWave class for object.

korrs avatar Feb 27 '18 20:02 korrs

Same here.

341bms avatar Mar 15 '18 12:03 341bms

me too

Go-Faram avatar Jul 31 '18 06:07 Go-Faram

It's common deserialization problem for objects which doesn't have class in current runtime. I think possible solution can be storing values as primitives(String, DateTime) which always exists.

korrs avatar Jul 04 '19 11:07 korrs

I know this is pretty old but still open so could be helpful anyway.

I got this Psych monkey patch and add what @korrs mentioned about CarrierWave and could load my audits with uploaders instance properly.

Thanks a lot @korrs!

module Psych
  def self.load(yaml, filename = nil, max_retries = 100)
    retries = 0
    uploader = nil
    begin
      result = Psych.parse(yaml, filename)
      result ? result.to_ruby : result
    rescue ArgumentError => e
      retries += 1
      if e.message.include?('undefined class/module') && retries < max_retries
        name = e.message.sub 'undefined class/module ', ''
        parts = name.split '::'
        if parts.length == 1
          klass = parts.only
          mod = Module
        else
          klass = parts[-1]
          mod = parts[0..-2].join('::').constantize
        end
        uploader = mod if klass.include?('Uploader')
        mod.const_set(klass, uploader)
        retry
      end
      raise e
    end
  end
end

guizaols avatar May 20 '20 19:05 guizaols