Created
December 2, 2008 19:07
-
-
Save shanesveller/31222 to your computer and use it in GitHub Desktop.
Delete all Items not referred to by at least one Toon
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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