mongoid-history icon indicating copy to clipboard operation
mongoid-history copied to clipboard

NameError: wrong constant name

Open c0mrade opened this issue 7 years ago • 5 comments

I ll describe everything I've done just so I m not missing anything obvious, here is what I've done so far :

  1. Created a class :
# app/models/history_tracker.rb
class HistoryTracker
  include Mongoid::History::Tracker
end
  1. Added an initializer :
# config/mongoid_history.rb
Mongoid::History.tracker_class_name = :history_tracker
  1. Added tracking to my model
class Agreement
  include Mongoid::Document
  include Mongoid::Timestamps
  include Mongoid::History::Trackable
  include Mongoid::Attributes::Dynamic

  field :licencing_fee, type: Integer
  field :q1_dollars, type: Integer
  field :q2_dollars, type: Integer
  field :q3_dollars, type: Integer
  field :q4_dollars, type: Integer

  track_history   :on => [:licencing_fee, :q2_dollars, :q2_dollars, :q3_dollars, :q4_dollars],     # track title and body fields only, default is :all
                  :modifier_field => :modifier, 
                  :modifier_field_inverse_of => :nil, 
                  :version_field => :version,        
                  :track_create   =>  true,          
                  :track_update   =>  true,        
                  :track_destroy  =>  true   
end
  1. When performing save! on my model I set the modifier:
user = ....
agreement = Agreement.new
agreement.modifier = user
agreement.licencing_fee = 500
agreement.save!
  1. Then save! method produces this error :
NameError: wrong constant name 
from /Users/nedstark/workspace/agreement-guide/vendor/bundle/gems/activesupport-5.0.1/lib/active_support/inflector/methods.rb:261:in `const_get'

What am I doing wrong here?

c0mrade avatar May 04 '17 03:05 c0mrade

What's the stack here? Check that the model is actually loaded (just stick a puts inside history_tracker.rb).

dblock avatar May 05 '17 11:05 dblock

@dblock I encountered the same problem. It seems that, by default, the model HistoryTracker wasn't loaded. I had to add the mongoid_history.rb in the initializer (which the documentation states is optional).

h8rry avatar Aug 01 '17 16:08 h8rry

Interesting, this probably is a bug, would appreciate a PR to fix it. Maybe we can even have a spec for this?

dblock avatar Aug 03 '17 22:08 dblock

Definitely the tracking history problem I encountered this problem and after disabling the tracking temporarily, the error was gone. @h8rry is right!!!

sumandroid avatar May 06 '18 19:05 sumandroid

This problem occurence because only include the module Mongoid::History::Tracker not call the your block included. So how tracker_class_name is nil, the line raises error NameError: wrong constant name https://github.com/mongoid/mongoid-history/blob/8ed7ba1e9d047c12cfe4605e9b65636956ed6a17/lib/mongoid/history/trackable.rb#L67-L68

some possible solutions are:

  • creates the initializer file.
  • or activate eager_load config.eager_load = true in file config/enviroments/development.rb

ArmandoAssuncao avatar Dec 28 '18 03:12 ArmandoAssuncao