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

#attributes is sometimes Hash and sometimes BSON::Document

    • Type: Icon: Bug Bug
    • Resolution: Duplicate
    • Priority: Icon: Unknown Unknown
    • None
    • Affects Version/s: 7.4.0
    • Component/s: Attributes
    • None

      `attributes` returns a `Hash` or a `Bson::Document` depending on what method was used to fetch the object. It's a hash after `create` and `reload`, but a `Bson::Document` after find. This means that attributes can't be consistently accessed with symbols as keys.

       

      ```ruby

      class Test

        include Mongoid::Document

      end

      > t = Test.create

      => #<Test _id: 627a49ef69ca9cc6b8bacb59, >

      > t.attributes.class

      => Hash

      > t1 = Test.find('627a49ef69ca9cc6b8bacb59')

      => #<Test _id: 627a49ef69ca9cc6b8bacb59, >

      > t1.attributes.class

      => BSON::Document

      > t1.attributes[:_id]

      => BSON::ObjectId('627a49ef69ca9cc6b8bacb59')

      > t1.reload.attributes.class

      => Hash

      > t1.reload.attributes[:_id]

      => nil

      ```

       

       

       

            Assignee:
            oleg.pudeyev@mongodb.com Oleg Pudeyev (Inactive)
            Reporter:
            vicmargar@gmail.com Víctor Martínez
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: