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

Change usage of ActiveModel::MissingAttributeError to Mongoid::Errors:AttributeNotLoaded

    • Type: Icon: Improvement Improvement
    • Resolution: Fixed
    • Priority: Icon: Unknown Unknown
    • 9.0.0
    • Affects Version/s: None
    • Component/s: None
    • None
    • None
    • Minor Change
    • None
    • None
    • None
    • None
    • None
    • None

      Our usage of ActiveModel::MissingAttribute is quite different than ActiveRecord's usage/intention.

      Mongoid raises ActiveModel::MissingAttribute when using the .only projection method, like so:

       

      Band.only(:name).first.label

       

       

      However, Rails raises this error when attempting to call a getter which is not defined. From the Rails docs:

       

        # Raised when an attribute is not defined.
        #
        #   class User < ActiveRecord::Base
        #     has_many :pets
        #   end
        #
        #   user = User.first
        #   user.pets.select(:id).first.user_id
        #   # => ActiveModel::MissingAttributeError: missing attribute: user_id

       

      In this same case, Mongoid would simply raise a plain-old MethodMissing error.

      I propose to rename this error to Mongoid::Errors::AttributeNotLoaded to better clarify its intention. In addition, we should:

      • Have this new error class inherit from Mongoid::Error::MongoidError, same as every other error in Mongoid.
      • Address MONGOID-5320 (make error message more informative)

            Assignee:
            oleg.pudeyev@mongodb.com Oleg Pudeyev (Inactive)
            Reporter:
            shields@tablecheck.com Johnny Shields
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved:
              None
              None
              None
              None