Skip to content

Instantly share code, notes, and snippets.

@dmoulton
Forked from kfatehi/HstoreModel.rb
Created June 12, 2012 19:39
Show Gist options
  • Save dmoulton/2919670 to your computer and use it in GitHub Desktop.
Save dmoulton/2919670 to your computer and use it in GitHub Desktop.
Hstore ActiveRecord::Base method_missing
## Hstore Method Missing Extension
module HstoreModel
def method_missing(method, *args, &block)
key = method.to_s.gsub('=','').to_sym
sym_options = options.symbolize_keys
if !self.class.attribute_methods_generated?
self.class.define_attribute_methods
if respond_to_without_attributes?(method)
send(method, *args, &block)
else
super
end
elsif sym_options[key]
if method[-1] == "=" #setter
self.class.send :define_method, method do
binding.pry
sym_options[key] = args
end
else # getter
self.class.send :define_method, method do
sym_options[key]
end
end
self.send(method, *args, &block)
else
super
end
end
end
## Thing.rb
class Thing < ActiveRecord::Base
include HstoreModel
end
# I can now get/set Thing's hstore attributes ('data' is an hstore attribute on Thing) as though they were "native". Yay it works with Formtastic now
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment