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

Eliminate x = (attributes[:foo] ||= value) pattern

    • Type: Icon: Improvement Improvement
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 7.1.2, 7.0.8
    • Affects Version/s: None
    • Component/s: None

      The following pattern is problematic:

      x = (attributes[:foo] ||= value)
      

      This is because x receives value, not whatever ends up in attributes[:foo] after the value is converted to indifferent access. This was reported previously and again in https://jira.mongodb.org/browse/MONGOID-4874.

      carbon% git g '(.*||=.*)'
      lib/mongoid/association/referenced/belongs_to.rb:          (@inverse_type ||= "#{name}_type") if polymorphic?
      lib/mongoid/atomic.rb:      (delayed_atomic_pulls[document.association_name.to_s] ||= []).push(document)
      lib/mongoid/atomic.rb:      (delayed_atomic_unsets[document.association_name.to_s] ||= []).push(document)
      lib/mongoid/criteria/modifiable.rb:          (@create_attrs ||= {}).merge!(attrs)
      lib/mongoid/extensions/hash.rb:            (consolidated[key] ||= {}).merge!(value)
      lib/mongoid/extensions/hash.rb:            (consolidated["$set"] ||= {}).merge!(key => mongoize_for(key, klass, key, value))
      lib/mongoid/fields.rb:            (attributes[name] ||= {}).with_indifferent_access
      lib/mongoid/persistable/pushable.rb:            existing = send(field) || (attributes[field] ||= [])
      lib/mongoid/persistable/pushable.rb:            existing = send(field) || (attributes[field] ||= [])
      lib/mongoid/query_cache.rb:          (@cached_documents ||= []).concat(documents)
      

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

              Created:
              Updated:
              Resolved: