Datamapper paranoid delete quirks

Posted April 15, 2010

Recently discovered some quirks while working with Datamapper (ver 0.10.2). If you want to fetch deleted objects you need to use the syntax:

Model.with_deleted { Model.all }

Which by the way will only find deleted objects, if you want all the objects your going to have to do something like

Model.with_deleted { Model.all } | Model.all

The next quirky bit is if you want to undelete things you can't simply do:

m = Model.with_deleted { Model.get(1) }
m.update( :deleted_at => nil )

You actually have to do:

m = Model.with_deleted { Model.get(1) }
m.deleted_at
m.update( :deleted_at => nil )

Due to the lazy loading that Datamapper does it actually doesn't load the :deleted_at property until you have accessed and apparently won't save changes to it either.

Full example:

class Test
  include DataMapper::Resource
  
  property :id, Serial
  property :deleted_at, ParanoidDateTime
end

t = Test.new
t.save                          #~ (0.354887) INSERT INTO "tests" DEFAULT VALUES
t = Test.new
t.save                          #~ (0.089852) INSERT INTO "tests" DEFAULT VALUES
t = Test.first                  #~ (0.000191) SELECT "id" FROM "tests" WHERE "deleted_at" IS NULL ORDER BY "id" LIMIT 1
t.destroy                       #~ (0.002124) UPDATE "tests" SET "deleted_at" = '2010-04-15T17:02:14-07:00' WHERE "id" = 1
t = Test.with_deleted { Test.first }  #~ (0.000092) SELECT "id" FROM "tests" WHERE NOT("deleted_at" IS NULL) ORDER BY "id" LIMIT 1
t.deleted_at = nil
t.save                          # true
t = Test.with_deleted { Test.first }  #~ (0.000094) SELECT "id" FROM "tests" WHERE NOT("deleted_at" IS NULL) ORDER BY "id" LIMIT 1
t.deleted_at                    #~ (0.000429) SELECT "id", "deleted_at" FROM "tests" WHERE "id" = 1 ORDER BY "id"

Comments

There are no comments for this post.

No comments found

Add comment

Music

Bad Ass Bitchby 2 Live CrewPlayed on 2010/09/02 at 09:20PM
Sie ist ein ungeheur, deine tochterby Richard StraussPlayed on 2010/09/02 at 09:19PM
Thank You (feat. Patti LaBelle)by DMXPlayed on 2010/09/02 at 09:16PM
It's So Laughableby Doris DayPlayed on 2010/09/02 at 09:13PM
Long Gone Bluesby Billie HolidayPlayed on 2010/09/02 at 09:10PM