diff --git a/app/services/tournament_services/tournament_backup_service.rb b/app/services/tournament_services/tournament_backup_service.rb index d189c27..cd678e0 100644 --- a/app/services/tournament_services/tournament_backup_service.rb +++ b/app/services/tournament_services/tournament_backup_service.rb @@ -41,7 +41,11 @@ class TournamentBackupService mat_assignment_rules: @tournament.mat_assignment_rules.map do |rule| rule.attributes.merge( mat: Mat.find_by(id: rule.mat_id)&.attributes.slice("name"), - weight_classes: rule.weight_classes.map do |weight_id| + # Emit the human-readable max values under a distinct key to avoid + # colliding with the raw DB-backed "weight_classes" attribute (which + # is stored as a comma-separated string). Using a different key + # prevents duplicate JSON keys when symbols and strings are both present. + "weight_class_maxes" => rule.weight_classes.map do |weight_id| Weight.find_by(id: weight_id)&.max end ) diff --git a/app/services/tournament_services/wrestlingdev_importer.rb b/app/services/tournament_services/wrestlingdev_importer.rb index 2c18440..91d4caf 100644 --- a/app/services/tournament_services/wrestlingdev_importer.rb +++ b/app/services/tournament_services/wrestlingdev_importer.rb @@ -86,17 +86,30 @@ class WrestlingdevImporter mat_assignment_rules.each do |rule_attributes| mat_name = rule_attributes.dig("mat", "name") mat = Mat.find_by(name: mat_name, tournament_id: @tournament.id) - - # Map max values of weight_classes to their new IDs - new_weight_classes = rule_attributes["weight_classes"].map do |max_value| - Weight.find_by(max: max_value, tournament_id: @tournament.id)&.id - end.compact - - # Extract bracket_positions and rounds + + # Prefer the new "weight_class_maxes" key emitted by backups (human-readable + # max values). If not present, fall back to the legacy "weight_classes" + # value which may be a comma-separated string or an array of IDs. + if rule_attributes.key?("weight_class_maxes") && rule_attributes["weight_class_maxes"].respond_to?(:map) + new_weight_classes = rule_attributes["weight_class_maxes"].map do |max_value| + Weight.find_by(max: max_value, tournament_id: @tournament.id)&.id + end.compact + elsif rule_attributes["weight_classes"].is_a?(Array) + # Already an array of IDs + new_weight_classes = rule_attributes["weight_classes"].map(&:to_i) + elsif rule_attributes["weight_classes"].is_a?(String) + # Comma-separated IDs stored in the DB column; split into integers. + new_weight_classes = rule_attributes["weight_classes"].to_s.split(",").map(&:strip).reject(&:empty?).map(&:to_i) + else + new_weight_classes = [] + end + + # Extract bracket_positions and rounds (leave as-is; model will coerce if needed) bracket_positions = rule_attributes["bracket_positions"] rounds = rule_attributes["rounds"] - - rule_attributes.except!("id", "mat", "tournament_id", "weight_classes") + + # Remove any keys we don't want to mass-assign (including both old/new weight keys) + rule_attributes.except!("id", "mat", "tournament_id", "weight_classes", "weight_class_maxes") MatAssignmentRule.create( rule_attributes.merge(