0

我有 4 个模型:Drawer、Folder、FolderDocument 和 Document,如下所示:

class Drawer < ActiveRecord::Base
  has_many :folders #Drawer has many folders
end

class Folder < ActiveRecord::Base
  belongs_to :drawer
  has_many :folder_documents

  # Folder has a "version" attribute which reflects the latest version
  # Use proc to give back latest version by default e.g. folder.documents or folder.documents(5) will give back a specific version.      
  has_many :documents, :through => :folder_documents, :conditions => proc { |v = nil|
         v ||= self.version
         "documents.active IS TRUE AND version = #{v}"
       }, :uniq => true 
end

class FolderDocument < ActiveRecord::Base
  # Has a version attribute
  belongs_to :document
  belongs_to :folder
end

class Document < ActiveRecord::Base
  has_many :folder_documents
  has_many :folders, :through => :folder_documents
end

我的问题是我无法创建

has_many :documents, :through => :folders

在类 Drawer 上,作为 proc 条件(对于来自 FolderDocument 的文档)不能计算,因为“版本”是在 Drawer 的上下文中计算的,而不是中间文件夹关联。

有没有办法我可以做到这一点,而无需在 Folder 和 FolderDocuments 之间创建另一个名为 FolderVersion 的模型?

编辑:目标是获取属于当前版本文件夹的所有文档(文件夹中的版本)。

4

0 回答 0