This project is archived and is in readonly mode.
new_record? returns nil on records retrieved from database
-
Steve Martocci
actually this is a better solution as the other one will return true on falses not just nils
def new_record?
(answer = super).nil? ? true : answerend
-
Peter Jaros
I'm seeing
model.new_record? == nilfor an AR object I got with a simplefind(id). I'm not sure if it's related. It's certainly worrying, since it means that the object isn't==to other objects with the sameidandnew_record == false. -
Steve Martocci
I noticed that too, it actually causes my change not to work....
I overrode new_method? in the model I was doing joins on to just use this
hack to fix active record not populating this value from joins
def new_record?
id.nil? ? true : falseend
and it works for my one specific case.
-
Lar Van Der Jagt
This bit me as well. After discussing with Steve it seems that 'new_record?' only works as expected with records which have just been instantiated. If you retrieve a record via 'find', calling 'new_record?' on that object will always return nil.
Not sure if this is a bug or just an unfortunate side effect of ActiveRecord, but it does seem to violate the principle of least surprise.
-
Lar Van Der Jagt
- Title changed from new_record? not set when using joins to new_record? returns nil on records retrieved from database
-
Yaroslav Markin
- Tag changed from activerecord, create, joins to activerecord, create, patch, tiny
Patch attached, I believe that it should return false on existing records as well, unless we have a good reason not to
-
Yaroslav Markin
- Assigned user set to DHH
-
Repository
- State changed from new to committed
(from [6e98adfc8e19a39fa45d4acd94145d318d151964]) ActiveRecord::Base#new_record? now returns false for existing records (was nil) [#1219 new_record? returns nil on records retrieved from database state:committed]
Signed-off-by: David Heinemeier Hansson david@loudthinking.com http://github.com/rails/rails/co...
