Created
March 30, 2013 07:06
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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