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

Find by id doesn't refresh associations

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

      I was trying to find out why one of my specs was passing when it should not, I found this behavior which I believe is a bug.

      If I have this models:

      class Foo
        include Mongoid::Document
      
        has_many :dependencies
      
        def add_dependency
          self.dependencies << Dependency.new
        end
      
      end
      

      and

      class Dependency
        include Mongoid::Document
      
        belongs_to :foo
      
        field :name
      
        validates_presence_of :name
      end
      

      After calling the add_dependency in a foo's instance, the dependecies array is not empty.
      Check the code to reproduce this:

      
      Foo.create!
      
      f = Foo.first
      f.add_dependency # won't save foo because of the dependency validation
      f2 = Foo.find(f.id)
      f2.dependencies
      #=> [#<Dependency _id: 513a534650b3683fcf000006, foo_id: "513a52f650b3683fcf000005", name: nil>]
      Dependency.count
      #=> 0
      
      f3 = Foo.last
      f3.dependencies
      #=> []
      
      f.save
      #=> false
      

      I don't think this is intuitive and would classify that as a bug.

            Assignee:
            Unassigned Unassigned
            Reporter:
            felipero Felipe Rodrigues de Almeida
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: