Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 轨道3。通过关联查找具有多个类别的文档_Ruby On Rails_Ruby_Associations_Kaminari - Fatal编程技术网

Ruby on rails 轨道3。通过关联查找具有多个类别的文档

Ruby on rails 轨道3。通过关联查找具有多个类别的文档,ruby-on-rails,ruby,associations,kaminari,Ruby On Rails,Ruby,Associations,Kaminari,我有以下型号 Document has_many :document_categorizations has_many :document_categories, through: :document_categorizations DocumentCategory has_many :document_categorizations has_many :documents, through: :document_categorizations DocumentCategorization

我有以下型号

Document
has_many :document_categorizations
has_many :document_categories, through: :document_categorizations

DocumentCategory
has_many :document_categorizations
has_many :documents, through: :document_categorizations

DocumentCategorization
belongs_to :document_category
belongs_to :document
在我的索引操作中,我可以按类别筛选文档

def index
  if params[:category_id].nil?
    @documents = Document.page(params[:page]).per(15)
  else
    @documents = DocumentCategory.find(params[:category_id]).documents
    @category = DocumentCategory.find(params[:category_id])
  end    
  ....
end
我不能再使用
DocumentCategory.find(params[:category_id]).documents
,因为我刚刚添加了kaminari用于分页,我需要在文档模型而不是DocumentCategory处进行查询


如何查询特定类别的文档?啊!很容易。这就是连续几个小时编写代码时发生的情况。我想我只是需要休息一下,从中获得一点看法,突然间,它击中了我

@category = DocumentCategory.find(params[:category_id])
@documents = @category.documents.page(params[:page]).per(15)

你想完成什么?看起来好像
DocumentCategorization
是不必要的等等,我以为
kaminari
在处理关系。似乎没有什么理由不起作用。我遗漏了什么吗?@varatis我需要文档分类,因为一个文档可以属于许多类别。@Azolo我目前拥有的东西可以工作。但是如何设置
DocumentCategory.find(params[:category_id]).documents
的分页,或者我需要修改该查询以向其添加分页。@leonel看起来这是一种HABTM情况,而您没有正确设置表。