0

I have the following models

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

In my index action, I can filter the documents by category...

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

I can't use DocumentCategory.find(params[:category_id]).documents anymore because I just added kaminari for pagination and I need to make the query at the Document model not DocumentCategory.

How can I query for documents of a certain category?

4

1 に答える 1

1

ああ!とても簡単。数時間ぶっ通しでコーディングすると、このようなことが起こります。ちょっと休憩して、少し視野を広げる必要があったと思うのですが、突然それが私を襲ったのです。

@category = DocumentCategory.find(params[:category_id])
@documents = @category.documents.page(params[:page]).per(15)
于 2012-07-03T19:19:59.223 に答える