Skip to content

Instantly share code, notes, and snippets.

@gamov
Created October 14, 2011 05:55
Show Gist options
  • Save gamov/1286351 to your computer and use it in GitHub Desktop.
Save gamov/1286351 to your computer and use it in GitHub Desktop.
Adding setter and getter of attributes of a AR model backed by an Hash
class MyModel < ActiveRecord::Base
serialize :preferences, Hash
PREFS_KEYS = [:hostname, :has_retail]
PREFS_KEYS.each do |key|
define_method(key) do
preferences[key]
end
define_method("#{key}=") do |value|
preferences[key]= value
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment