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

Custom ids: saving a new document with an id that already belongs to an existing document

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

      When using custom ids and saving a document that has an id that already matches a saved document, the save fails but says it succeeds. No error is raised, and the document reports being persisted, but nothing is actually saved to the db.

      Here's a simple example:

      class Day
      include Mongoid::Document
      field :_id, type: Date, default: ->

      { Date.today }

      field :note, type: String
      end

      Day.count # 0

      d1 = Day.new note: "d1"
      d1.id # Sun, 27 Oct 2013

      d2 = Day.new note: "d2"
      d2.id # Sun, 27 Oct 2013

      d1.save # returns true
      Day.count # 1
      d2.save # also returns true. No error is raised.
      d2.persisted? # true – says it's persisted... but...
      Day.count # still 1

      Day.find(Date.today).note # "d1" – only the first document was saved

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

              Created:
              Updated:
              Resolved: