Skip to content

Instantly share code, notes, and snippets.

@hartbit
Created December 29, 2010 17:30
Show Gist options
  • Save hartbit/758785 to your computer and use it in GitHub Desktop.
Save hartbit/758785 to your computer and use it in GitHub Desktop.
DataMapper ManyToMany Update
class Device
include DataMapper::Resource
property :id, Serial
property :udid, String, :required => true
has n, :apps, :through => Resource
end
class App
include DataMapper::Resource
property :id, Serial
property :bundle_id, String, :required => true
has n, :devices, :through => Resource
end
bundle_id = app_data['bundle_id']
udids = app_data['udids']
result = ""
app = App.first_or_create(:bundle_id => bundle_id)
app.devices.delete_if { |device| !udids.index(device.udid) }
udids.each do |udid|
found_device = false
app.devices.each do |device|
if device.udid == udid then
found_device = true
break
end
end
if !found_device then
result += "Add device '#{udid}'\n"
device = Device.first_or_create(:udid => udid)
device.apps << app
device.save!
end
end
app.save!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment