Created
December 29, 2010 17:30
-
-
Save hartbit/758785 to your computer and use it in GitHub Desktop.
DataMapper ManyToMany Update
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 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 |
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
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