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

HABTM relationship cached even after setting ids and saving?

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

      class Parent
        include Mongoid::Document
      
        has_and_belongs_to_many :childs, :inverse_of => nil, :validate => false
      end
      
      class Child
        include Mongoid::Document
        
        field :hi, :type => String
      end
      

      Mongoid 2.4.1, Rails 3.1.1

      nader$ rails c
      Loading development environment (Rails 3.1.1)
      ruby-1.9.2-p290 :001 > p1 = Parent.create
       => #<Parent _id: 4f7348308035440918000001, _type: nil, child_ids: []> 
      ruby-1.9.2-p290 :002 > p1.childs
       => [] 
      ruby-1.9.2-p290 :003 > p1.child_ids = [Child.first.id, Child.last.id]
       => [BSON::ObjectId('4f7346d980354408da000001'), BSON::ObjectId('4f73472380354408da000004')] 
      ruby-1.9.2-p290 :004 > p1.save
       => true 
      ruby-1.9.2-p290 :005 > p1.childs
       => [] 
      ruby-1.9.2-p290 :006 > p1.childs.count
       => 2 
      ruby-1.9.2-p290 :007 > p1.childs.length
       => 0 
      

            Assignee:
            Unassigned Unassigned
            Reporter:
            nader Nader Akhnoukh
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: