shanesveller (owner)

Fork Of

Revisions

gist: 31222 Download_button fork
public
Description:
Delete all Items not referred to by at least one Toon
Public Clone URL: git://gist.github.com/31222.git
Embed All Files: show embed
item.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
items.rb #
1
2
3
4
5
6
7
8
9
10
11
12
# 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
toon.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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