1
我正在編寫一個插件,其中我在插件中動態定義了一個新的關係。下面如何獲取保存在實例變量中的模型的屬性
module AttachDocumentsAs
@as = nil
def attach_documents_as(*attachment_as)
attachment_as = attachment_as.to_a.flatten.compact.map(&:to_sym)
@as = attachment_as.first
class_inheritable_reader(@as)
class_eval do
has_many @as, :as => :attachable, :class_name=>"AttachDocuments::Models::AttachedDocument"
accepts_nested_attributes_for @as
end
end
end
現在示例代碼給出任何模式,我把它作爲
class Person < AtiveRecord::Base
attach_documents_as :financial_documents
end
現在要訪問要訪問這個類的屬性,重載initialize方法這樣
def initialize(*args)
super(*args)
"#{@as}".build
end
但它沒有獲得必需的屬性,任何人都可以幫助我。我想建立這種關係並設置一些初始值。
等待所有你們的指導方針。
我用 class_inheritable_reader(@as) class_inheritable_reader(:atd_as) write_inheritable_attribute(:atd_as,@as) 現在裏面初始化我可以得到它作爲 self.send(self.send( :atd_as)) 但現在的問題是調用構建它 self.send(self.send(:atd_as))建立 不起作用。 –