rails: save (ActiveRecord::Persistence) can return true even when the model is not saved, and seems to break the principle of "least surprise"
Imported from Lighthouse. Original ticket at: http://rails.lighthouseapp.com/projects/8994/tickets/6666 Created by Alexey Muranov - 2011-04-04 19:14:57 UTC
These two examples illustrate the unexpected (to me) behavior of save
method in ActiveRecord::Persistence
for a sample application created with
$ rails new test_app
$ cd test_app
$ rails generate model Person name:string
$ rake db:migrate
First example. In console:
p = Person.create(:name=>'Bill')
p.destroy
p.name # => "Bill"
p.save # => true
Despite the true
returned by save
, the database is empty in the end.
Second example. In console:
p = Person.create(:name=>'Bill')
Person.find(p.id).destroy
p.persisted? # => true
p.destroyed? # => false
p.name = "John" # => "John"
p.save # => true
Again, despite the true
returned by save
, the database is empty in the end.
This problem was discussed by me and others here: http://www.ruby-forum.com/topic/1386349
I saw other tickets about unexpected behavior of save
, but didn’t find an exact match to this problem.
About this issue
- Original URL
- State: closed
- Created 13 years ago
- Reactions: 1
- Comments: 17 (4 by maintainers)
is this still not fixed? I get the same behaviour as of Rails 5 beta 2.