Skip to content

Instantly share code, notes, and snippets.

@pixeltrix
Created March 30, 2013 07:06
Show Gist options
  • Save pixeltrix/5275703 to your computer and use it in GitHub Desktop.
Save pixeltrix/5275703 to your computer and use it in GitHub Desktop.
Example of using a custom coder to read and write an array based attribute
class ArrayCoder
def initialize(default = [])
@default = default
end
def dump(object)
Array(object).join(',')
end
def load(string)
string.present? ? string.split(',') : @default.clone
end
end
class Order < ActiveRecord::Base
serialize :items, ArrayCoder.new
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment