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

save on an object with no changes triggers validations

    XMLWordPrintable

    Details

    • Type: Task
    • Status: Closed
    • Resolution: Done
    • Affects Version/s: None
    • Fix Version/s: 3.1.3
    • Component/s: None
    • Labels:
      None

      Description

      I'm not sure if this is by design or a bug, but if you have an object without changes, and do a save against it, all validations are run (and additional queries executed as necessary to validate relations), but no insert is then performed.

      /mongoid/persistence/operations/update.rb

      has this:

              def persist
                prepare do
                  unless updates.empty?
                    collection.find(selector).update(positionally(selector, updates))
                    ...
      

      Why not do

      def persist
         return if updates.empty?
         prepare do
          ...
      

      I imagine that the consequence of this would be that save on an invalid object without changes would return true, which is potentially breaking, but since the end result is the same (no update to the object in the DB), it seems like save is equally valid as a return result.

        Attachments

          Activity

            People

            Assignee:
            Unassigned
            Reporter:
            msaffitz Michael Saffitz
            Participants:
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved: