0

單桌玩圍繞繼承hstore在軌道4,5的方法,但得到一個錯誤試圖調用方法父時:Rails的NoMethodError時,呼籲家長

# 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 

錯誤:

Exception encountered: #<NoMethodError: undefined method `getter_setter_for' for #<Class:0x007f8c739a5ae0>> 

我忽略了什麼明顯的東西?

回答

1

getter_setter_for(key)是一種實例方法。但它在課堂上被召喚出來。它將在類範圍內的DiskItem類加載期間調用。

+0

doh!謝謝! 'def self.getter_setter_for(key)'修正了它! – Meltemi