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情况,而您没有正确设置表。