# Save after delete returns true but actually fails

**URL:** <https://www.ruby-forum.com/t/save-after-delete-returns-true-but-actually-fails/159076>\
**Category:** Rails\
**Created:** [January 31, 2009, 8:34am UTC](https://www.ruby-forum.com/t/save-after-delete-returns-true-but-actually-fails/159076 "2009-01-31T08:34:06Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![farmerss](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/dbc845/32.png) [@farmerss](https://www.ruby-forum.com/u/farmerss)\
**Post date:** [January 31, 2009, 8:34am UTC](https://www.ruby-forum.com/t/save-after-delete-returns-true-but-actually-fails/159076/1 "2009-01-31T08:34:06Z")

</div>

Hi,  
I’m using Rails 2.2.2. I noticed that creating two references to the  
same database ActiveRecord object, then deleting with one of them,  
followed by saving with the other, true is returned, but no new record  
is actually saved. e.g.:

doc=Document.new(:title =\> “sam’s speciality”)  
doc.save  
doc.id # ==\> 23  
doc2=Document.find\_by\_title(“sam’s speciality”)  
doc2.delete  
doc.title # ==\> “sam’s speciality”  
doc.save # ==\> true  
doc3=Document.find\_by\_title(“sam’s speciality”) # ==\> nil

Should that second doc.save really return true? This situation could  
easily enough arise if two separate users are accessing the same record  
simultaneously.

The same thing occurs when the model is using optimistic locking. It  
seems to me that in this case, it would be nice if the second doc.save  
raised an exception.  
(If one makes a change to one of doc’s fields immediately before the  
second save, then one does get an ActiveRecord::StaleObjectError  
exception.)

- Farmer

---

<div class="post-metadata">

**Author:** ![farmerss](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/dbc845/32.png) [@farmerss](https://www.ruby-forum.com/u/farmerss)\
**Post date:** [February 2, 2009, 9:01pm UTC](https://www.ruby-forum.com/t/save-after-delete-returns-true-but-actually-fails/159076/2 "2009-02-02T21:01:50Z")

</div>

> The same thing occurs when the model is using optimistic locking. It  
> seems to me that in this case, it would be nice if the second doc.save  
> raised an exception.  
> (If one makes a change to one of doc’s fields immediately before the  
> second save, then one does get an ActiveRecord::StaleObjectError  
> exception.)

Does this still happen if you turn off partial updates for the class?

Document.partial\_updates = false

# that stuff

I think it’s just the partial updates code detecting you haven’t saved  
anything and ‘optimising’ the call to save.

–  
Cheers

Koz
