has_one referencing undefined class passes validation when validating a new model instance

XMLWordPrintableJSON

    • None
    • None
    • None
    • None
    • None
    • None

      The behavior between a has_one and has_many association differs if an association class is missing (i.e. if `Widget` is not defined):

      Has One:

      class User
        include Mongoid::Document
        has_one :widget
      end
      
      User.new.valid? # true
      

      Has Many:

      class User
        include Mongoid::Document
        has_many :widgets
      end
      
      User.new.valid? # raises NameError (uninitialized constant Widget)
      

      This only impacts new records. For example:

      User.create
      User.last.valid? # correctly raises an exception
      

              Assignee:
              Unassigned
              Reporter:
              Kevin Sylvestre
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

                Created:
                Updated: