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

save on an object with no changes triggers validations

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

      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.

      Unable to find source-code formatter for language: lib. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      /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.

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

              Created:
              Updated:
              Resolved: