从人员记录中,我需要能够添加/显示特定类型的文档。包括 category_id 的条件是可行的,但在这一点上,我不能假设类别 ID 会随着开发的进展而保持不变。这也是我只创建我需要的类别的测试的问题。
有没有办法可以动态设置category_id,例如这样的:
has_many :personal_documents, :as => :documentable, :conditions => "category_id = #{DocumentCategory.find_by_name('Personal').id}", class_name: 'Document'
楷模:
Person < AR::Base
has_many :documents, :as => :documentable
has_many :personal_documents, :as => :documentable, #:conditions => "category_id = 1"
has_many :legal_documents, :as => :documentable, #:conditions => "category_id = 2"
end
Animal < AR::Base
has_many :documents, :as => :documentable
end
Document < AR::Base
belongs_to :person
belongs_to :category
end