mongoid-history
mongoid-history copied to clipboard
NameError: wrong constant name
I ll describe everything I've done just so I m not missing anything obvious, here is what I've done so far :
- Created a class :
# app/models/history_tracker.rb
class HistoryTracker
include Mongoid::History::Tracker
end
- Added an initializer :
# config/mongoid_history.rb
Mongoid::History.tracker_class_name = :history_tracker
- 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
- When performing
save!
on my model I set the modifier:
user = ....
agreement = Agreement.new
agreement.modifier = user
agreement.licencing_fee = 500
agreement.save!
- 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?
What's the stack here? Check that the model is actually loaded (just stick a puts
inside history_tracker.rb).
@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).
Interesting, this probably is a bug, would appreciate a PR to fix it. Maybe we can even have a spec for this?
Definitely the tracking history problem I encountered this problem and after disabling the tracking temporarily, the error was gone. @h8rry is right!!!
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 fileconfig/enviroments/development.rb