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

Clarify that association assignment saves documents

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 7.5.0
    • Affects Version/s: 6.2.1
    • Component/s: Associations
    • Environment:
      MacOS, Ruby 2.3.5
    • Fully Compatible

      I would expect that calling doc.reload would reload all embedded documents as well. This does not seem to be the case.

      Given these documents:

      class Foo
        include Mongoid::Document
        field :foo, type: String
        embeds_many :bar
      end
      
      class Bar
        include Mongoid::Document
        field :bar, type: String
      end
      

      This piece of code highlights the issue:

      Foo.create!(foo: 'foo1', bar: [Bar.new(bar: 'bar1')])
      doc = Foo.first
      doc.bar = [Bar.new(bar: 'bar2')]
      doc.foo = 'foo2'
      p "-- After change"
      p doc.foo
      p doc.bar.first.bar
      doc.reload
      p "-- After reload"
      p doc.foo
      p doc.bar.first.bar
      doc.save!
      p "-- What's in the DB?"
      p Foo.first.foo
      p Foo.first.bar.first.bar
      

            Assignee:
            oleg.pudeyev@mongodb.com Oleg Pudeyev (Inactive)
            Reporter:
            carl.vander@valtech.se Carl Vander
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: