Skip to content

Instantly share code, notes, and snippets.

@marklocklear
Created June 28, 2012 10:46
Show Gist options
  • Save marklocklear/3010629 to your computer and use it in GitHub Desktop.
Save marklocklear/3010629 to your computer and use it in GitHub Desktop.
class Product < ActiveRecord::Base
#serialize :data, ActiveRecord::Coders::Hstore
attr_accessible :name, :category, :price, :description
validates_numericality_of :runtime, allow_blank: true
%w[author rating runtime].each do |key|
attr_accessible key
scope "has_#{key}", lambda { |value| where("properties @> (? => ?)", key, value) }
define_method(key) do
properties && properties[key]
end
define_method("#{key}=") do |value|
self.properties = (properties || {}).merge(key => value)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment