Вопрос

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?

Это было полезно?

Решение

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.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top