Skip to content

Instantly share code, notes, and snippets.

@rwilcox
Created August 27, 2011 19:58
Show Gist options
  • Select an option

  • Save rwilcox/1175801 to your computer and use it in GitHub Desktop.

Select an option

Save rwilcox/1175801 to your computer and use it in GitHub Desktop.
An awesome way forward with serialized attributes
# WITH HASHIE (https://github.com/intridea/hashie)
# ######################################################
class User
before_create :default_preferences_value
serialize :preferences_hash
delegate :full_name, :to => :preferences_hash
delegate :full_name=, :to => :preferences_hash
delegate :timezone, :to => :preferences_hash
delegate :timezone=, :to => :preferences_hash
delegate :favorite_color, :to => :preferences_hash
delegate :favorite_color=, :to => :preferences_hash
def default_preferences_value
self.preferences_hash = Hashie::Mash.new if preferences_hash.nil?
end
end
@rwilcox

rwilcox commented Aug 27, 2011

Copy link
Copy Markdown
Author

PS: yes, I know about simple_eav. I also have a long history of trying to get simple_eav working on projects. This is a reasonable fallback, or when the needs expand beyond simple eav (for example, hashie's coercion)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment