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

uninitialized constant User

Open lgmkr opened this issue 10 years ago • 11 comments

Hello, My stack: sinatra (1.4.5) mongoid (3.1.6) mongoid-history (0.4.4)

Initializer:

Mongoid::History.tracker_class_name = :history_tracker

I have a models:

class HistoryTracker
  include Mongoid::History::Tracker
end

class Event
  include Mongoid::Document
  include Mongoid::Timestamps
  include Mongoid::Search
  include Mongoid::Paperclip

  ### event history
  include Mongoid::History::Trackable

  ......

  track_history   :on => :all,      
                  :version_field  => :version,  
                  :track_create   =>  true,   
                  :track_update   =>  true     
.......
end

class ProviderNetwork
  include Mongoid::Document
  include Mongoid::Timestamps
  include Mongoid::Enum
  ........
end

I try:

 Event.create!(name: "hello name", modifier: ProviderNetwork.last)
NameError: uninitialized constant User
from ..../activesupport-3.2.19/lib/active_support/inflector/methods.rb:230:in `block in constantize'

I don't use mongoid_userstamp gem.
What I doing wrong ? I need your help

lgmkr avatar Dec 22 '14 18:12 lgmkr

Do you have a User model in your app? Try to find the call stack of this exception?

dblock avatar Dec 25 '14 21:12 dblock

I get uninitialized constant User as well after putting Mongoid History in a gem I am contributing to. I may well have just configured incorrectly but I'm stumped none the less.

In the Gem, I have models Content and User.

Namespacing should be CMS::Content and CMS::User

I have:

class Content
    belongs_to :author, class_name: 'CMS::User', :inverse_of => :authored_contents
    track_history :track_create => true, :track_destroy => true, :modifier_field => :author
end

Removing :modifier_field => whatever works, but then obviously the History object has nil for who modified it.

i.e.:

track_history :track_create => true, :track_destroy => true

works... but:

track_history :track_create => true, :track_destroy => true, :modifier_field => :author

throws uninitialized constant User

Partial stack trace:

13:54:34 web.1       | NameError - uninitialized constant User:
13:54:34 web.1       |   activesupport (4.1.8) lib/active_support/dependencies.rb:519:in `load_missing_constant'
13:54:34 web.1       |   activesupport (4.1.8) lib/active_support/dependencies.rb:180:in `const_missing'
13:54:34 web.1       |   activesupport (4.1.8) lib/active_support/inflector/methods.rb:238:in `block in constantize'
13:54:34 web.1       |   activesupport (4.1.8) lib/active_support/inflector/methods.rb:236:in `constantize'
13:54:34 web.1       |   activesupport (4.1.8) lib/active_support/core_ext/string/inflections.rb:66:in `constantize'
13:54:34 web.1       |    () Users/stephenmarsh/.rvm/gems/ruby-2.1.5/bundler/gems/mongoid-60c4d942df30/lib/mongoid/relations/metadata.rb:575:in `klass'
13:54:34 web.1       |    () Users/stephenmarsh/.rvm/gems/ruby-2.1.5/bundler/gems/mongoid-60c4d942df30/lib/mongoid/relations/builders/referenced/in.rb:19:in `build'
13:54:34 web.1       |    () Users/stephenmarsh/.rvm/gems/ruby-2.1.5/bundler/gems/mongoid-60c4d942df30/lib/mongoid/relations/accessors.rb:43:in `create_relation'
13:54:34 web.1       |    () Users/stephenmarsh/.rvm/gems/ruby-2.1.5/bundler/gems/mongoid-60c4d942df30/lib/mongoid/relations/accessors.rb:26:in `__build__'
13:54:34 web.1       |    () Users/stephenmarsh/.rvm/gems/ruby-2.1.5/bundler/gems/mongoid-60c4d942df30/lib/mongoid/relations/accessors.rb:104:in `block (2 levels) in get_relation'
13:54:34 web.1       |    () Users/stephenmarsh/.rvm/gems/ruby-2.1.5/bundler/gems/mongoid-60c4d942df30/lib/mongoid/threaded/lifecycle.rb:125:in `_loading'
13:54:34 web.1       |    () Users/stephenmarsh/.rvm/gems/ruby-2.1.5/bundler/gems/mongoid-60c4d942df30/lib/mongoid/relations/accessors.rb:100:in `block in get_relation'
13:54:34 web.1       |    () Users/stephenmarsh/.rvm/gems/ruby-2.1.5/bundler/gems/mongoid-60c4d942df30/lib/mongoid/threaded/lifecycle.rb:84:in `_building'
13:54:34 web.1       |    () Users/stephenmarsh/.rvm/gems/ruby-2.1.5/bundler/gems/mongoid-60c4d942df30/lib/mongoid/relations/accessors.rb:99:in `get_relation'
13:54:34 web.1       |    () Users/stephenmarsh/.rvm/gems/ruby-2.1.5/bundler/gems/mongoid-60c4d942df30/lib/mongoid/relations/accessors.rb:187:in `block in getter'

Seems like constantize is looking for “User” when it should be looking for "CMS::User" ??

stephenhmarsh avatar Jan 05 '15 19:01 stephenhmarsh

Update: could be related to this line: https://github.com/aq1018/mongoid-history/pull/127/files#diff-bcdd27320396c76a2b2c2b20b0b65dd2R34 ???

Commenting those lines out it no longer throws error...

Then setting track_history :modifier_field => :author throws error it can't find 'Author' class...

But...

track_history ::modifier_field => :cms_author throws no error, however my history objects still have :modifier => nil for some reason??

stephenhmarsh avatar Jan 05 '15 20:01 stephenhmarsh

Figured it out, just needed to set: Mongoid::History.modifier_class_name = 'CMS::User'

In my config/initializers/mongoid-history.rb

Pretty obvious, but would love to see this make its way into the documentation.

stephenhmarsh avatar Jan 06 '15 16:01 stephenhmarsh

@stephenhmarsh can you please raise a PR to help us improve the docs?

johnnyshields avatar Jan 06 '15 16:01 johnnyshields

@johnnyshields will do!

stephenhmarsh avatar Jan 06 '15 16:01 stephenhmarsh

I get the same error and I don't have a User class in my project. Is it possible to use this gem without a modifier?

SebastianSzturo avatar Nov 23 '15 11:11 SebastianSzturo

@SebastianSzturo Does setting Mongoid::History.modifier_class_name = nil work? We should document it, too and/or add support for that. Would appreciate a PR either way.

dblock avatar Nov 23 '15 13:11 dblock

Mongoid::History.modifier_class_name = nil is also not working. :( I get the error `uninitialized constant Modifier``. It seams like a modifier is necessary.

SebastianSzturo avatar Nov 23 '15 17:11 SebastianSzturo

@SebastianSzturo Try writing a spec that reproduces this and the fix should be fairly trivial to skip the modifier logic on nil.

dblock avatar Nov 23 '15 18:11 dblock

We still want this, maybe someone cares to help?

dblock avatar Jun 20 '18 13:06 dblock