How to get attribute of a model saved in instance variable
Posted
by
Nazar
on Stack Overflow
See other posts from Stack Overflow
or by Nazar
Published on 2011-01-12T14:52:24Z
Indexed on
2011/01/12
14:53 UTC
Read the original article
Hit count: 253
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.
© Stack Overflow or respective owner