0

Have an old project that someone did in PHP and am attempting to convert it over to Rails. Am encountering an issue when trying to iterate through a multiple array in rails. The array as stored in the DB looks like this:

a:5:{i:0;s:8:"Director";i:1;s:11:"Shareholder";i:2;s:14:"Vice President";i:3;s:9:"Secretary";i:4;s:9:"Treasurer";}

Am trying to display the String values such as "Director" and "Shareholder". Does the DB field need to be changed to a different format to work? How would this be done in rails?

Thank you in advance

mu is too short
  • 426,620
  • 70
  • 833
  • 800

1 Answers1

0

That looks like the output of PHP's serialize function to me.

If you have PHP around or don't mind installing it to help with the data migration, then I'd write a short PHP script to convert those columns to JSON using PHP's unserialize and json_encode functions: read a value, unserialize it, json_encode the result, write it back into the database (all in PHP).

After that, you should be able to use ActiveRecord's serialize with JSON as the storage format:

class Model < ActiveRecord::Base
  serialize :whatever_it_is_called, JSON
end

You could also use YAML (the default for ActiveRecord's serialize) if you had YAML support in PHP-land.

If you don't want to touch PHP at all, then you'll have to write Ruby parser for PHP's serialize format (which seems to be well documented in the PHP docs) and hook that up to ActiveRecord's serialize.

You will of course be working with a copy of the real database for all this work.

Community
  • 1
  • 1
mu is too short
  • 426,620
  • 70
  • 833
  • 800