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

Adding code and spec to fix blindness to callbacks that return false during updates

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

      Although failed validations cause updates to return false (thereby enabling the standard Rails idiom of "if @object.save" in the update action of a controller), callbacks that return false do not cause @object.save to return false. On new records, when a callback returns false, @object.save correctly returns false.

      In the real world, this means that a create action will work normally, but an update action will not. The best workaround I've come up with is to use .save! for everything (and then raising exceptions in my callbacks when I want them to fail), and then catching exceptions in my controllers rather than using "if @object.save". But this is not optimal.

      I've made a very small change to lib/mongoid/persistence/update.rb to prevent the persist method from returning true in cases when a callback has returned false. I also added a corresponding test.

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

              Created:
              Updated:
              Resolved: