This project is archived and is in readonly mode.

#1219 ✓committed
Steve Martocci

new_record? returns nil on records retrieved from database

Reported by Steve Martocci | October 15th, 2008 @ 05:14 PM | in 2.x

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

Comments and changes to this ticket

  • Steve Martocci

    Steve Martocci October 17th, 2008 @ 04:41 PM

    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

  • Peter Jaros

    Peter Jaros October 20th, 2008 @ 04:20 PM

    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.

  • Steve Martocci

    Steve Martocci October 20th, 2008 @ 04:33 PM

    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.

  • Lar Van Der Jagt

    Lar Van Der Jagt October 22nd, 2008 @ 09:39 PM

    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

    Lar Van Der Jagt October 22nd, 2008 @ 09:42 PM

    • Title changed from “new_record? not set when using joins” to “new_record? returns nil on records retrieved from database”
  • Yaroslav Markin

    Yaroslav Markin December 27th, 2008 @ 01:28 PM

    • 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

    Yaroslav Markin December 27th, 2008 @ 01:31 PM

    • Assigned user set to “DHH”
  • Repository

    Repository December 27th, 2008 @ 01:36 PM

    • State changed from “new” to “committed”

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

    Signed-off-by: David Heinemeier Hansson david@loudthinking.com http://github.com/rails/rails/co...

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Attachments

Referenced by

Pages