Uploaded image for project: 'Mongoid'
  1. Mongoid
  2. MONGOID-3682

Added an error for persisting with non-existing subclass type.

    • Type: Icon: Task Task
    • Resolution: Won't Fix
    • Priority: Icon: Major - P3 Major - P3
    • 5.0.2
    • Affects Version/s: None
    • Component/s: None
    • Labels:
      None

      I got an error when creating a new document with non-existing subclass type.
      Here is an example. Boy and Girl are subclasses of Kid, and Dog is not existing class.

      $ rails c
      Loading development environment (Rails 4.0.0)
      2.0.0-p451 :001 > Boy
       => Boy 
      2.0.0-p451 :002 > Kid.create _type: "Boy"
       => #<Kid _id: 538f3bcd75627503f4000000, _type: "Boy"> 
      2.0.0-p451 :003 > Girl
       => Girl 
      2.0.0-p451 :004 > Kid.create _type: "Girl"
       => #<Kid _id: 538f3bd475627503f4010000, _type: "Girl"> 
      2.0.0-p451 :005 > Kid.last
       => #<Girl _id: 538f3bd475627503f4010000, _type: "Girl"> 
      2.0.0-p451 :006 > Kid.create _type: "Dog"
       => #<Kid _id: 538f3bf175627503f4020000, _type: "Dog"> 
      2.0.0-p451 :007 > Kid.last
      NameError: uninitialized constant Dog
      	from /usr/local/rvm/gems/ruby-2.0.0-p451/gems/activesupport-4.0.0/lib/active_support/inflector/methods.rb:226:in `const_get'
      	from /usr/local/rvm/gems/ruby-2.0.0-p451/gems/activesupport-4.0.0/lib/active_support/inflector/methods.rb:226:in `block in constantize'
      	from /usr/local/rvm/gems/ruby-2.0.0-p451/gems/activesupport-4.0.0/lib/active_support/inflector/methods.rb:224:in `each'
      	from /usr/local/rvm/gems/ruby-2.0.0-p451/gems/activesupport-4.0.0/lib/active_support/inflector/methods.rb:224:in `inject'
      	from /usr/local/rvm/gems/ruby-2.0.0-p451/gems/activesupport-4.0.0/lib/active_support/inflector/methods.rb:224:in `constantize'
      	from /usr/local/rvm/gems/ruby-2.0.0-p451/gems/activesupport-4.0.0/lib/active_support/core_ext/string/inflections.rb:66:in `constantize'
      	from /usr/local/rvm/gems/ruby-2.0.0-p451/bundler/gems/mongoid-fdd963e15bfd/lib/mongoid/factory.rb:44:in `from_db'
      	from /usr/local/rvm/gems/ruby-2.0.0-p451/bundler/gems/mongoid-fdd963e15bfd/lib/mongoid/relations/eager.rb:16:in `with_eager_loading'
      	from /usr/local/rvm/gems/ruby-2.0.0-p451/bundler/gems/mongoid-fdd963e15bfd/lib/mongoid/contextual/mongo.rb:282:in `block (2 levels) in last'
      	from /usr/local/rvm/gems/ruby-2.0.0-p451/bundler/gems/mongoid-fdd963e15bfd/lib/mongoid/contextual/mongo.rb:556:in `with_inverse_sorting'
      	from /usr/local/rvm/gems/ruby-2.0.0-p451/bundler/gems/mongoid-fdd963e15bfd/lib/mongoid/contextual/mongo.rb:281:in `block in last'
      	from /usr/local/rvm/gems/ruby-2.0.0-p451/bundler/gems/mongoid-fdd963e15bfd/lib/mongoid/contextual/mongo.rb:447:in `try_cache'
      	from /usr/local/rvm/gems/ruby-2.0.0-p451/bundler/gems/mongoid-fdd963e15bfd/lib/mongoid/contextual/mongo.rb:280:in `last'
      	from /usr/local/rvm/gems/ruby-2.0.0-p451/bundler/gems/mongoid-fdd963e15bfd/lib/mongoid/contextual.rb:20:in `last'
      	from /usr/local/rvm/gems/ruby-2.0.0-p451/bundler/gems/mongoid-fdd963e15bfd/lib/mongoid/findable.rb:133:in `last'
      	from (irb):7
      	from /usr/local/rvm/gems/ruby-2.0.0-p451/gems/railties-4.0.0/lib/rails/commands/console.rb:90:in `start'
      	from /usr/local/rvm/gems/ruby-2.0.0-p451/gems/railties-4.0.0/lib/rails/commands/console.rb:9:in `start'
      	from /usr/local/rvm/gems/ruby-2.0.0-p451/gems/railties-4.0.0/lib/rails/commands.rb:64:in `<top (required)>'
      	from bin/rails:4:in `require'
      	from bin/rails:4:in `<main>'2.0.0-p451 :008 > 
      

      It is not so easy to find out the cause of the problem when that error message was shown. Moreover, the case below can be occurred.

      [1] pry(main)> Kid.destroy_all
      NameError: uninitialized constant Dog
      from /usr/local/rvm/gems/ruby-2.0.0-p451/gems/activesupport-4.0.0/lib/active_support/inflector/methods.rb:226:in `const_get'
      

      In this pull-req, I added an error that is raised when creating a new document with non-existing subclass type.
      Although many other ways to handle that problem might be there, anyway, defining the behavior for this case should be needed.

      Thank you.

            Assignee:
            dianna.hohensee@mongodb.com Dianna Hohensee (Inactive)
            Reporter:
            johnny-miyake johnny-miyake
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: