Using `only` can cause an association reset

XMLWordPrintableJSON

    • Type: Task
    • Resolution: Done
    • 3.0.2
    • Affects Version/s: None
    • Component/s: None
    • None
    • None
    • None
    • None
    • None
    • None
    • None

      Consider the following models

      class User
        include Mongoid::Document
      
        has_and_belongs_to_many :tags, inverse_of: nil
        field :email
        field :social_profile
        # ... other user fields
      end
      
      class Tag
        include Mongoid::Document
        # ... tag fields, nothing fancy here
      end
      

      The following code updates the social_profile field using only to limit the amount of data loaded

      User.where(social_profile: nil).only(:email).each do |user|
        user.social_profile = lookup_service.load_profile(user.email)
        user.save!
      end
      

      This loop results in tags being reset on all matching users except for the first one. FWIW it used to keep tags for all users in Mongoid 2.x

              Assignee:
              Unassigned
              Reporter:
              Alex Tsibulya
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

                Created:
                Updated:
                Resolved: