Can no longer create new accounts due to old multi-select user field plugin

He’s lucky that I’m just up the road :wink:.

The data format was interesting - it was in two different forms which changed one day. They started out as:

this
that

And one day they suddenly changed to

this,that

I did some ugly spreadsheet wizardry to transform the data into how the new multiselect fields are (as above). Unfortunately, the script can’t (yet) handle multiselect. PR anyone?

Although I can workaround / hack it to do the job I reckon:

1st pass: all users with a single entry using the existing script
2nd pass: all users with multiple entries using a hacked script which forces new rows.

later…
My cunning plan worked.

This is the hacked script for the 2nd pass:

# frozen_string_literal: true

require "csv"
desc "Import user fields"
task "multi_user_fields:import_csv", [:filename] => [:environment] do |_, args|

  puts "Filename: #{args[:filename]}"
  data = CSV.read(args[:filename], headers: true );

  data.each_entry do |row|
    puts "doing row."
    row.to_h.each do |x|
      user_id = row['user_id']
      if x.first == 'user_id'
        u = User.find(user_id)
        puts "Got user: #{u.username}"
      else
        name = x.first
        val = row[x.first]
        ucf = UserCustomField.find_by(user_id: row['user_id'], name: name)
          if val
            puts "Creating UCF: #{row['user_id']} Name: #{name}, value: #{val}"
            UserCustomField.create(user_id: user_id, name: name, value: val)
          end
      end
    end
  end
end

@pfaffman - how easy would it be to update the actual script to handle multi-select fields for others who mess with UCFs in the future?

4 Likes