我有一個場景,我需要一個複雜的多態關係,並且需要幫助。複雜的多態關係
我有AA模式「一號文件」,它可以連接到任何模型,如「員工」,「網友」等
我開發一個插件來連接不同的文件
class Employee < ActiveRecord::Base
attach_documents_as :general_documents
attach_documents_as :meeting_documents
end
直到這一點,我成功了。但爲了獲得更多,我需要爲文檔分配一些類型,因爲它需要像這樣在主模型中提供文檔類型的模型名稱。
class Employee < ActiveRecord::Base
attach_documents_as :general_documents, :general_doc_types
attach_documents_as :meeting_documents, :meeting_doc_types
attach_documents_as :job_record, :misc_doc_types
end
這裏
:general_doc_types , :meeting_doc_types, :misc_doc_types
都是型號名稱。
我的文檔模型這樣
class Document < ActiveRecord::Base
attr_accessible :title, :description, :attachment
belongs_to :attachable, :polymorphic => true
has_attached_file :attachment
end
任何一個可以有想法,我怎麼能我做出的類型不同型號的文件關係。將尋找您的反饋。
那麼`general_doc_types`等模型會由您的生成器或用戶自己生成嗎?如果是前一種情況,你是否考慮過讓魔法變成簡單的`has_many`和`belongs_to`? – PeterWong 2011-06-11 15:57:17