Skip to content

Instantly share code, notes, and snippets.

@kibaekr
Created September 26, 2014 05:21
Show Gist options
  • Save kibaekr/3225f20dbcb0858a0558 to your computer and use it in GitHub Desktop.
Save kibaekr/3225f20dbcb0858a0558 to your computer and use it in GitHub Desktop.
user
# add_column :users, :data_key, :string
# add_column :users, :data_value, :string
DATA_KEYS = ['vehicle_type', 'city', 'phone_model', 'availability', 'example_customer_service']
DATA_KEYS.each do |method|
define_method(method) do
find_by_data_key(method).try(:data_value)
end
# setter method
define_method("#{method}=") do |value|
data = find_or_initialize_by_data_key(method)
data.data_value = value.strip
data.save
end
end
u= User.first
u.city = "San Francisco"
NoMethodError: undefined method `find_or_initialize_by_data_key' for #<User:0x00000109a7da00>
u.city= NoMethodError: undefined method `find_by_data_key' for #<User:0x00000109a7da00>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment