Skip to content

Instantly share code, notes, and snippets.

@MarkMT
Created March 4, 2010 15:20
Show Gist options
  • Save MarkMT/321796 to your computer and use it in GitHub Desktop.
Save MarkMT/321796 to your computer and use it in GitHub Desktop.
class Remote < Application
def download
ok = true
[<a bunch of model names...>].each do |model|
# Get the remote records for the model
records = []
repository(:remote) {records = model.all}
# Copy the records to the default repository
records.each do |record|
# check whether the record is already in the default repository
if record.id && copy = model.get(record.id)
ok = false unless copy.update(record.attributes)
else
unless !record.id && copy = model.first(record.attributes)
# note that you can't create a new record directly with the 'record' attributes, as create will
# try to insert a sequential id that may clash with one that's already copied. You have to make a new
# record in memory, replace all the attributes and then save it.
copy = model.new
copy.attributes = record.attributes
ok = false unless copy.save
end
end
end
end
message[:result] = ok ? "Data downloaded ok" : "We had a problem"
render
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment