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

Overridden field accessors don't work properly for array types

    • Type: Icon: Task Task
    • Resolution: Done
    • 12_01_17
    • Affects Version/s: None
    • Component/s: None
    • Labels:
      None

      In order to save space on default field values I've overridden methods in my models to ensure nil values return a default values when read. This works find for all types but Array. When I try to push a new value to the array the new value isn't being set.

      Given the following model:

      class User
        field :friend_ids, type: Array
      
        def friend_ids
          read_attribute(:friend_ids) || []
        end
      end
      

      When running the following in the console you can see how friend_ids isn't being set properly.

      > u = User.new
      > u.friend_ids
      => []
      > u.friend_ids << '123'
      => ["123"]
      > u.friend_ids
      => []
      

      Is there a better way of dealing with this situation or is it a bug that can be fixed?

            Assignee:
            Unassigned Unassigned
            Reporter:
            fbjork Fredrik Björk
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: