I have a scenario where i needed a complex polymorphic relation, and need help in it.
I have a a model "Document", which can be attached to any model, like "Employee", "User" etc.
I am developing a plugin to attach different documents
class Employee < Acti开发者_运维问答veRecord::Base
attach_documents_as :general_documents
attach_documents_as :meeting_documents
end
Till this point i got succeed. But to get more i need to assign some type to the document, for it i want provide the model name of document type from with in main model like that.
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
Here
:general_doc_types , :meeting_doc_types, :misc_doc_types
are all model names.
I have the document model like this
class Document < ActiveRecord::Base
attr_accessible :title, :description, :attachment
belongs_to :attachable, :polymorphic => true
has_attached_file :attachment
end
Can any one have idea that how i can i make relation of document to different models of types. Will be looking for your feedback.
It looks like you're creating a polymorphic relationship using single table inheritance. I searched for this a little and didn't come up with too many solutions, which makes me wonder if there's a better approach. I haven't done this before so I can't really say for sure. One example I did find looks like it is pretty straightforward to setup. http://www.mendable.com/2010/03/30/polymorphic-single-table-inheritance-sti.html
精彩评论