Forum: Ruby on Rails How to determine if an object has just been created

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
631d1677786f0725da6ccee46043686e?d=identicon&s=25 Jonathan Viney (Guest)
on 2006-05-19 09:51
(Received via mailing list)
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.
Ad7805c9fcc1f13efc6ed11251a6c4d2?d=identicon&s=25 Alex Young (Guest)
on 2006-05-19 10:07
(Received via mailing list)
Jonathan Viney 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?
42172acdf3c6046f84d644cb0b94642c?d=identicon&s=25 Pat Maddox (pergesu)
on 2006-05-19 10:41
(Received via mailing list)
On 5/19/06, Jonathan Viney <jonathan.viney@gmail.com> wrote:
> -Jonathan.
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> 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
Ec5a599777854c540fd102ef4691fe10?d=identicon&s=25 Rimantas Liubertas (Guest)
on 2006-05-19 11:31
(Received via mailing list)
> 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
42172acdf3c6046f84d644cb0b94642c?d=identicon&s=25 Pat Maddox (pergesu)
on 2006-05-19 11:47
(Received via mailing list)
On 5/19/06, Rimantas Liubertas <rimantas@gmail.com> 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
631d1677786f0725da6ccee46043686e?d=identicon&s=25 Jonathan Viney (Guest)
on 2006-05-19 13:43
(Received via mailing list)
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.
This topic is locked and can not be replied to.