Home > hashed_attributes

hashed_attributes

Hashed_attributes is a project mainly written in ..., it's free.

Adds convenient getters and setters via serialized hash on ActiveRecord models

= Hashed Attributes

Requires a column to be used for a serialized hash

db/migrations/****_create_people.rb

t.text :preferences

In the model you declare the column name and a list of attributes the hash will store

app/models/person.rb

class Person < ActiveRecord::Base hashed_attributes :preferences, :theme, :plan, :favorite_color end

== Initialization Initializes preferences as a hash Person.new { :id => nil, :name => nil, :preferences => {}, :emails => nil, :created_at => nil, :updated_at => nil }

== Usage Hash keys are made available as getter/setter methods on the model

person = Person.new(:theme=>"molokai", :plan => "pro", :favorite_color=>"orange") { :id => nil, :name => nil, :preferences => { :favorite_color => "orange", :theme => "molokai", :plan => "pro" }, :emails => nil, :created_at => nil, :updated_at => nil }

Methods can be used normally and are stored in the preferences hash person.theme = 'the blue theme' person.plan = 'pro account' person.favorite_color = 'black'

person.theme

=> 'the blue theme'

Previous:Twelve