0

I have an active record class with an embedded sample:

class LabResults < ActiveRecord::Base
  serialize :sample
end

class Sample
  attr_accessor :values    # GSL::Vector of responses

  def to_yaml
    YAML.quick_emit( self, opts ) { |out|
      out.map( "!testfile,2012-02-27" ) { |map|
        @values.map{|v| v.to_a }
      }
    }
  end

  def analyze; end;    # do stuff with values
end

I want to serialize and store sample in the database, but GSL::Vector (from gsl gem), does not have a to_yaml method. Defining to_yaml and YAML.quick_emit for Sample is apparently deprecated when using Rails 3.2's default YAML engine Psych.

Any ideas how to serialize and de-serialize this object?

Allyl Isocyanate
  • 13,306
  • 17
  • 79
  • 130

1 Answers1

0

You can write a custom (de)serializer for the column, and pass it as the second argument to "serialize", e.g.:

serialize :sample, SampleSerializer.new

Where SampleSerializer is a class that defines "load" and "dump" methods.

More detail in this answer: ActiveRecord serialize using JSON instead of YAML

Community
  • 1
  • 1
Brian
  • 359
  • 2
  • 5