Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

new_record? returns nil on records retrieved from database

#1219

I am using a right join to ensure that I always pull back a set of objects via an association even if they haven't been created yet. They have some fields filled in by the join but the ids are blank. When I go to save the object it attempts to do an update instead of a save.

This is because @new_record is not being set, in fact a new_record? returns nil instead of true or false. I added this to my model to ensure a true if it is not set.

def new_record? super || true end

I do not think new_record should be able to return nil, it should return true if @new_record is not set. The alternative is to properly set @new_record to true in this case

Reported by Steve Martocci · October 15th, 2008 @ 05:14 PM

State: committed
Milestone: 2.x
Assigned to: DHH DHH
Importance: none

Activity

  1. Steve Martocci
    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 : answer
    
    

    end

    October 17th, 2008 @ 04:41 PM

  2. Peter Jaros
    Peter Jaros

    I'm seeing model.new_record? == nil for an AR object I got with a simple find(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 same id and new_record == false.

    October 20th, 2008 @ 04:20 PM

  3. Steve Martocci
    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 : false
    
    

    end

    and it works for my one specific case.

    October 20th, 2008 @ 04:33 PM

  4. Lar Van Der Jagt
    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.

    October 22nd, 2008 @ 09:39 PM

  5. Lar Van Der Jagt
    Lar Van Der Jagt
    • Title changed from new_record? not set when using joins to new_record? returns nil on records retrieved from database

    October 22nd, 2008 @ 09:42 PM

  6. Yaroslav Markin
    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

    December 27th, 2008 @ 01:28 PM

  7. Yaroslav Markin
  8. Repository
    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...

    December 27th, 2008 @ 01:36 PM