Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save notEthan/297243912fcbd07354fc3d48093df12f to your computer and use it in GitHub Desktop.
Save notEthan/297243912fcbd07354fc3d48093df12f to your computer and use it in GitHub Desktop.
# Setup
require 'active_record'
require 'arms'
dbfile = "foos.sqlite3"
File.unlink(dbfile) if File.exist?(dbfile)
ActiveRecord::Base.establish_connection({
:adapter => "sqlite3",
:database => dbfile,
})
ActiveRecord::Schema.define do
create_table :foos do |table|
table.column :preferences, :string
end
end
class Foo < ActiveRecord::Base
arms_serialize :preferences, :indifferent_hashes, [YAML, Hash]
end
# Runtime
Foo.create!(preferences: {favorite_animal: 'ocelot'})
foo = Foo.last
puts foo.preferences_before_type_cast
puts foo.preferences[:favorite_animal]
puts foo.preferences['favorite_animal']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment