From 6c7e8bc8a16b500460f5742f6e5681e24d64947c Mon Sep 17 00:00:00 2001 From: Agrim Mittal Date: Wed, 5 Apr 2017 01:31:05 +0530 Subject: [PATCH] Add rake task for resources --- lib/tasks/normalize_resources.rake | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 lib/tasks/normalize_resources.rake diff --git a/lib/tasks/normalize_resources.rake b/lib/tasks/normalize_resources.rake new file mode 100644 index 00000000..e05d0d69 --- /dev/null +++ b/lib/tasks/normalize_resources.rake @@ -0,0 +1,12 @@ +namespace :data do + desc 'Update resources with nil quantity/used fields' + task normalize_resources: :environment do + Resource.where('used is ? or quantity is ?', nil, nil).each do |resource| + resource.used = 0 if resource.used.blank? + resource.quantity = resource.used if resource.quantity.blank? + unless resource.save + puts "Failed to update resource #{resource.name} (ID #{resource.id})" + end + end + end +end