Reputation: 38359
Playing around with Single Table Inheritance and hstore in Rails 4 but getting an error when trying to call method in the parent:
# models/item.rb
class Item < ActiveRecord::Base
def getter_setter_for(key)
define_method(key) do
properties && properties[key]
end
define_method("#{key}=") do |value|
self.properties = (properties || {}).merge(key => value)
end
end
end
# models/disk_item.rb
class DiskItem < Item
%w[filename path].each do |key|
getter_setter_for(key) ## <== error calling this method
end
end
Error:
Exception encountered: #<NoMethodError: undefined method `getter_setter_for' for #<Class:0x007f8c739a5ae0>>
What obvious thing have I overlooked?
Upvotes: 0
Views: 463
Reputation: 29349
getter_setter_for(key) is an instance method. But its getting called at the class level. It will be called during the DiskItem class load on class scope.
Upvotes: 1