Save after delete returns true but actually fails


#1

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

#2

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