# How to determine if an object has just been created

**URL:** <https://www.ruby-forum.com/t/how-to-determine-if-an-object-has-just-been-created/61016>\
**Category:** Rails\
**Created:** [May 19, 2006, 9:51am UTC](https://www.ruby-forum.com/t/how-to-determine-if-an-object-has-just-been-created/61016 "2006-05-19T09:51:22Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jonny](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/47e85d/32.png) [@jonny](https://www.ruby-forum.com/u/jonny)\
**Post date:** [May 19, 2006, 9:51am UTC](https://www.ruby-forum.com/t/how-to-determine-if-an-object-has-just-been-created/61016/1 "2006-05-19T09:51:22Z")

</div>

Is there a built-in way to determine if a model object has just been  
created (ie. this object was the one that was originally persisted)? I  
can’t see it anywhere in the AR code, but thought I’d check. I want  
something like:

p = Person.create  
p.original\_instance? # true

Is this built-in or should I just patch AR?

-Jonathan.

---

<div class="post-metadata">

**Author:** ![jonny](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/47e85d/32.png) [@jonny](https://www.ruby-forum.com/u/jonny)\
**Post date:** [May 19, 2006, 10:07am UTC](https://www.ruby-forum.com/t/how-to-determine-if-an-object-has-just-been-created/61016/2 "2006-05-19T10:07:20Z")

</div>

Jonathan V. wrote:

> Is there a built-in way to determine if a model object has just been  
> created (ie. this object was the one that was originally persisted)? I  
> can’t see it anywhere in the AR code, but thought I’d check. I want  
> something like:
> 
> p = Person.create  
> p.original\_instance? # true
> 
> Is this built-in or should I just patch AR?  
> Not sure quite what you’re after, but does p.new\_record? do the trick?

---

<div class="post-metadata">

**Author:** ![jonny](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/47e85d/32.png) [@jonny](https://www.ruby-forum.com/u/jonny)\
**Post date:** [May 19, 2006, 10:41am UTC](https://www.ruby-forum.com/t/how-to-determine-if-an-object-has-just-been-created/61016/3 "2006-05-19T10:41:19Z")

</div>

On 5/19/06, Jonathan V. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> -Jonathan.
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

I don’t think there’s a built-in way to do that…but it seems like  
it’d be pretty simple.

def original\_instance?  
!new\_record? && created\_at == updated\_at  
end

and just add created\_at and updated\_at fields to your model.

Pat

---

<div class="post-metadata">

**Author:** ![jonny](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/47e85d/32.png) [@jonny](https://www.ruby-forum.com/u/jonny)\
**Post date:** [May 19, 2006, 11:31am UTC](https://www.ruby-forum.com/t/how-to-determine-if-an-object-has-just-been-created/61016/4 "2006-05-19T11:31:52Z")

</div>

> I don’t think there’s a built-in way to do that…but it seems like  
> it’d be pretty simple.

From documentation:

new\_record?  
Returns true if this object hasn’t been saved yet â?? that is, a record  
for the object doesn’t exist yet.

Regards,  
Rimantas

---

<div class="post-metadata">

**Author:** ![jonny](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/47e85d/32.png) [@jonny](https://www.ruby-forum.com/u/jonny)\
**Post date:** [May 19, 2006, 11:47am UTC](https://www.ruby-forum.com/t/how-to-determine-if-an-object-has-just-been-created/61016/5 "2006-05-19T11:47:19Z")

</div>

On 5/19/06, Rimantas L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > I don’t think there’s a built-in way to do that…but it seems like  
> > it’d be pretty simple.
> 
> From documentation:
> 
> new\_record?  
> Returns true if this object hasn’t been saved yet ? that is, a record  
> for the object doesn’t exist yet.

> From the OP:  
> “this object was the one that was originally persisted”

new\_record? tells you if the object hasn’t been saved. OP is asking  
how to tell if the record is the original one. My interpretation is  
basically that the object hasn’t been modified at all. The easiest  
way to do that (though perhaps not fool-proof) is to compare the  
updated\_at and created\_at fields. In fact, I used new\_record? in my  
code.

Another possibility is acts\_as\_versioned, which may be more air-tight  
than my solution (though I don’t know if there are any problems with  
it, I just came up with it).

Pat

---

<div class="post-metadata">

**Author:** ![jonny](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/47e85d/32.png) [@jonny](https://www.ruby-forum.com/u/jonny)\
**Post date:** [May 19, 2006, 1:43pm UTC](https://www.ruby-forum.com/t/how-to-determine-if-an-object-has-just-been-created/61016/6 "2006-05-19T13:43:16Z")

</div>

The reason for doing this is to be able to send emails when certain  
models are created and/or changed. I realised just after I made the  
post that I should use AR callbacks (after\_create and after\_update) on  
the models, and possibly an observer.

Thanks, -Jonathan.
