Created
June 28, 2012 10:46
-
-
Save marklocklear/3010629 to your computer and use it in GitHub Desktop.
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 | |
#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