Skip to content

Instantly share code, notes, and snippets.

@notEthan
Last active August 3, 2019 06:04
Show Gist options
  • Save notEthan/84a4e583ea6e96f0f92dab43286ba301 to your computer and use it in GitHub Desktop.
Save notEthan/84a4e583ea6e96f0f92dab43286ba301 to your computer and use it in GitHub Desktop.
# Setup
require 'active_record'
require 'arms'
ActiveRecord::Base.establish_connection(
:adapter => "sqlite3",
:database => "foos.sqlite3"
)
ActiveRecord::Schema.define do
create_table :foos do |table|
table.column :preferences, :string
end
end
class Foo < ActiveRecord::Base
arms_serialize :preferences, :indifferent_hashes, ::JSON
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