Skip to content

Instantly share code, notes, and snippets.

@jtsagata
Created April 24, 2012 21:11
Show Gist options
  • Save jtsagata/2483818 to your computer and use it in GitHub Desktop.
Save jtsagata/2483818 to your computer and use it in GitHub Desktop.
HStore
class Product < ActiveRecord::Base
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