Skip to content

Instantly share code, notes, and snippets.

@shanesveller
Created December 2, 2008 19:07
Show Gist options
  • Save shanesveller/31222 to your computer and use it in GitHub Desktop.
Save shanesveller/31222 to your computer and use it in GitHub Desktop.
Delete all Items not referred to by at least one Toon
class Item
include DataMapper::Resource
property :id, Integer, :key => true
property :name, String
property :icon_base, String
property :item_type, String
for prop in [:max_dura,:slot,:quality]
property prop, Integer
end
has n, :toons, :through => Resource
# ...
end
# Original credit goes to dkubb in #datamapper on freenode
def clean_orphaned
# delete all Items not referred to by at least one Toon
item_ids = Item.all(:fields => [ :id ]).map { |i| i.id }
item_ids -= ItemToon.all(:fields => [ :item_id ], :unique => true).map { |j| j.item_id }
Item.all(:id => item_ids).destroy!
# NOTE: a LEFT JOIN would probably be better, but this is
# the more efficient DataMapper approach I can think of
end
class Toon
include DataMapper::Resource
# ...
property :id, Serial
property :name, String
property :realm, String
property :guild, String, :lazy => [:show]
property :level, Integer
property :race, String, :lazy => [:show]
property :klass, String, :lazy => [:show]
property :armor, Integer, :lazy => [:show]
# ...
has n, :items, :through => Resource
# ...
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment