From 6de768c417369ad9f4e18a6c733b83e074841515 Mon Sep 17 00:00:00 2001 From: Rodrigo Souto Date: Wed, 8 Jan 2014 15:03:51 +0000 Subject: [PATCH] custom-forms: update answers with the alternatives ids --- plugins/custom_forms/db/migrate/20130823151900_associate_fields_to_alternatives.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+), 0 deletions(-) diff --git a/plugins/custom_forms/db/migrate/20130823151900_associate_fields_to_alternatives.rb b/plugins/custom_forms/db/migrate/20130823151900_associate_fields_to_alternatives.rb index 5ecf512..0cf7d67 100644 --- a/plugins/custom_forms/db/migrate/20130823151900_associate_fields_to_alternatives.rb +++ b/plugins/custom_forms/db/migrate/20130823151900_associate_fields_to_alternatives.rb @@ -14,6 +14,18 @@ class AssociateFieldsToAlternatives < ActiveRecord::Migration end end + CustomFormsPlugin::Answer.find_each do |answer| + labels = [] + answer.value.split(',').each do |value| + labels << answer.field.choices.invert[value] + end + labels.compact! + if labels.present? + answer.value = answer.field.alternatives.where('label IN (?)', labels).map(&:id).join(',') + answer.save! + end + end + change_table :custom_forms_plugin_fields do |t| t.remove :choices end -- libgit2 0.21.2