Jonathan Viney
2006-May-19 07:50 UTC
[Rails] How to determine if an object has just been created
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.
Alex Young
2006-May-19 08:05 UTC
[Rails] How to determine if an object has just been created
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? -- Alex
Pat Maddox
2006-May-19 08:39 UTC
[Rails] How to determine if an object has just been created
On 5/19/06, Jonathan Viney <jonathan.viney@gmail.com> 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? > > -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
Rimantas Liubertas
2006-May-19 09:31 UTC
[Rails] How to determine if an object has just been created
> 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 -- http://rimantas.com/
Pat Maddox
2006-May-19 09:44 UTC
[Rails] How to determine if an object has just been created
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
Jonathan Viney
2006-May-19 11:41 UTC
[Rails] How to determine if an object has just been created
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. On 5/19/06, Pat Maddox <pergesu@gmail.com> wrote:> 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 > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >