Created
April 24, 2012 21:11
-
-
Save jtsagata/2483818 to your computer and use it in GitHub Desktop.
HStore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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