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

Hash attribute access/read_attribute fails on embedded field when parent loaded with only

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Minor - P4 Minor - P4
    • 7.3.0
    • Affects Version/s: 7.0.8, 6.4.7
    • Component/s: Attributes
    • None
    • Fully Compatible

      The read_attribute (version 6) / read_raw_attribute (version 7) fails to return a field from an embedded model if the parent was loaded with only (and the embedded model is listed in only).

      Example:

      class Model
      include Mongoid::Document
      
      embeds_one :inner_model
      end
      
      class InnerModel
       include Mongoid::Document
      
      embedded_in :model
      
      field :name, type: String
      end
      
      model = Model.create
      model.create_inner_model(name: 'test')
      
      model = Model.only(:inner_model).first
      puts model['inner_model.name'] # <= this fails with ActiveModel::MissingAttributeError
      

       

      It works with only('inner_model.name'), which is also tested in attributes_spec, but it's not really what I want to do - if I include a whole embedded model, I expect I can read any of its fields.

       

      I'm willing to make a patch for both version 6 and 7, if you confirm that this is not intentional behavior.

       

            Assignee:
            oleg.pudeyev@mongodb.com Oleg Pudeyev (Inactive)
            Reporter:
            stanley90x Marek Stanczyk
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: