25 Aug '13, 8am

“ruby on rails - ActiveRecord serialize using JSON instead of YAML - Stack Overflow”

I wrote my own YAML coder, that takes a default. Here is the class: class JSONColumn def initialize(default={}) @default = default end # this might be the database default and we should plan for empty strings or nils def load(s) s.present? ? JSON.load(s) : @default.clone end # this should only be nil or an object that serializes to JSON (like a hash or array) def dump(o) JSON.dump(o || @default) end end Since load and dump are instance methods it requires an instance to be passed as the second argument to serialize in the model definition. Here's an example of it: class Person < ActiveRecord::Base validate :name, :pets, :presence => true serialize :pets, JSONColumn.new([]) end I tried creating a new instance, loading an instance, and dumping an instance in IRB, and it all seemed to work properly. I wrote a blog post about it, too.

Full article: http://stackoverflow.com/questions/2080347/activerecord-s...

Tweets

Sr Ruby on Rails Developer (2-4 yrs) in Karnataka

hirist.com 26 Aug '13, 1pm

Ruby on Rails Developer with strong Ruby knowledge and experience building highly scalable application. We are currently s...

New Job Alert: Full-Stack Ruby on Rails Enginee...

simplyhired.com 25 Aug '13, 1pm

We are looking for solid, full-stack, Ruby on Rails engineers who are passionate about creating transformative technologie...