Glenn,
I would agree with Adam's use of of the exists method to find out if a
record exists for a given object. However I also agree with your use of
frozen?, with one caveat: an object's attributes may be frozen for some
other reason. I tend to think there should be a public instance method
destroyed? which definitively tells you when an object has been destroyed.
This might be a good time for me to submit a feature request to the
Rails/ActiveRecord core group. For now, you can add the code below to
environment.rb or wherever you like. My implementation is as follows:
class ActiveRecord::Base
def destroyed?
not new_record? and not self.class.exists?(self.id)
end
end
Jordan Fowler
On 2/6/08, Adam Grant <[EMAIL PROTECTED]> wrote:
>
> Try ActiveRecord's exists? method. Haven't used it, but looks good.
>
> - Adam
>
> On Feb 6, 2008 11:34 AM, Glenn Little <[EMAIL PROTECTED]> wrote:
>
> >
> > What is the "best practice" for determining whether or not
> > a model object corresponds to a database record that has been
> > deleted?
> >
> > Say I have a collection of Book objects, some of which have
> > been removed from the database with:
> >
> > @book = Book.find(blah)
> > @book.destroy
> > ...
> > @books << @book # don't know at this point whether @book has been
> > destroyed
> >
> > Now when I iterate through @books, I'd like to detect the
> > ones that have been destroyed.
> >
> > It appears that the destroyed ones will be "frozen" (@book.frozen? ==
> > true),
> > but assuming that frozen == destroyed seems somehow indirect
> > and side-effecty. Is there a more straighforward way (short of
> > re-querying
> > the database) that I'm missing?
> >
> > Thanks...
> >
> > -glenn
> >
> >
> >
>
> >
>
--
Jordan A. Fowler
2621 First Ave Apt 5
San Diego, CA 92103
E-mail: [EMAIL PROTECTED]
Website: http://www.jordanfowler.com
Phone: 406.546.8055
--~--~---------~--~----~------------~-------~--~----~
SD Ruby mailing list
[email protected]
http://groups.google.com/group/sdruby
-~----------~----~----~----~------~----~------~--~---