How to get attribute of a model saved in instance variable
- by Nazar
I am writing a plugin, in which i define a new relation dynamically with in plugin. Sample code is given below
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
now in any model i used it as
class Person < AtiveRecord::Base
attach_documents_as :financial_documents
end
Now want to access want to access this attribute of the class in overloaded initialize method like this
def initialize(*args)
super(*args)
"#{@as}".build
end
But it is not getting required attribute, can any one help me in it. I want to build this relation and set some initial values.
Waiting for guidelines from all you guys.