ActiveRecord::Base#new_record? now returns false for existing records (was nil) [#1219 state:committed]

Signed-off-by: David Heinemeier Hansson <david@loudthinking.com>
This commit is contained in:
Yaroslav Markin 2008-12-27 16:26:13 +03:00 committed by David Heinemeier Hansson
parent 4f043a4838
commit 6e98adfc8e
3 changed files with 9 additions and 2 deletions

@ -1,5 +1,7 @@
*2.3.0/3.0*
* Fixed that ActiveRecord::Base#new_record? should return false (not nil) for existing records #1219 [Yaroslav Markin]
* I18n the word separator for error messages. Introduces the activerecord.errors.format.separator translation key. #1294 [Akira Matsuda]
* Add :having as a key to find and the relevant associations. [Emilio Tagua]

@ -2406,9 +2406,9 @@ def id=(value)
write_attribute(self.class.primary_key, value)
end
# Returns true if this object hasn't been saved yet -- that is, a record for the object doesn't exist yet.
# Returns true if this object hasn't been saved yet -- that is, a record for the object doesn't exist yet; otherwise, returns false.
def new_record?
defined?(@new_record) && @new_record
(defined?(@new_record) && @new_record) || false
end
# :call-seq:

@ -1198,6 +1198,11 @@ def test_boolean_cast_from_string
assert b_true.value?
end
def test_new_record_returns_boolean
assert_equal Topic.new.new_record?, true
assert_equal Topic.find(1).new_record?, false
end
def test_clone
topic = Topic.find(1)
cloned_topic = nil