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

When adding/removing an item from a relation the {relation_name}_ids_was property does not reflect the previous state

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

      Given two classes

      class Item
        include Mongoid::Document
      
        has_and_belongs_to_many :tags
      end
      
      class Tag
        include Mongoid::Document
      end
      

      If a tag is added to the item.tags collection. item.tag_ids_was returns the current list of ids, rather than the previous list of ids. The same holds true if a tag is removed.

      These tests demonstrate the failure

      describe "Mongoid relations" do
        let(:item){ Item.create() }
        let(:tag){ Tag.create() }
      
        context "when a tag is added" do
          it "reports tag_ids_was == []" do
            item.tags << tag
            item.tag_ids_was.should be_empty
          end
        end
      
        context "when a tag is removed" do
          before(:each){ item.tags.push(tag); item.save! }
          it "reports tag_ids_was == [tag.id]" do
            item.tags.delete tag
            item.tag_ids_was.should include(tag.id)
          end
        end
      end
      

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

              Created:
              Updated:
              Resolved: