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

Make fields of type Array or Hash initialize to empty Array or Hash (instead of nil)

    • Type: Icon: Task Task
    • Resolution: Done
    • 4.0.0 final
    • Affects Version/s: None
    • Component/s: None
    • Labels:
      None

      I found this to be very useful, but I don't know if it's in line with your overall strategy.

      Explanation: When using relations, a "many" reference (e.g.: has_many, embeds_many) initializes as an empty array, which allows you to use array-specific methods (e.g.: Array#<<) with no worries. This behavior is nice. When defining a regular field of type Array, it initializes as nil, so if you're not sure whether the array already has elements in it, then you can't use array-specific methods without first testing whether it actually is an array. A similar argument applies to operating on fields of type Hash. This update just sets Array and Hash fields as "empty" instead of nil.

            Assignee:
            Unassigned Unassigned
            Reporter:
            wyattisimo Jared Wyatt
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: