Forum: Ruby on Rails save after delete returns true but actually fails

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Sandworth M. (Guest)
on 2009-01-31 09:34
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
Michael K. (Guest)
on 2009-02-02 22:01
(Received via mailing list)
> 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
This topic is locked and can not be replied to.