Rails复杂has_many和has_many通过

时间:2013-02-20 20:51:48

标签: ruby-on-rails ruby-on-rails-3.2 rails-activerecord

我有4个模型:抽屉,文件夹,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中的文档)不能计算为&#34;版本&#34;在Drawer的上下文中计算,而不是中间文件夹关联。

我是否可以在不在Folder和FolderDocuments之间创建另一个名为FolderVersion的模型的情况下执行此操作?

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

0 个答案:

没有答案