Hacktoberfest 2026:メンテナが10月に向けて印を付けた、オープンで初心者向けの issue。 Hacktoberfest の issue を見る

How to filter by polymorphic resource association filter?

オープン
#1,324 コメント 0 件 リアクション 1 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
5/5
見積もり時間
1週間以上
初心者へのやさしさ
25/100
issue の種類
バグ
明瞭さ
説明が足りない
活発さ
停滞
技術スタック
rails, ruby
領域
api, backend

調査の方向性

DocumentsDocumentableResource の polymorphic documentable association の宣言と、2 つの filter request の例から始めます。JSONAPI Resources が polymorphic relationship のフィルターをどのように検証するかを追跡し、type-specific dispatch がサポートされているかを判断します。完了条件は、適切な filter を呼び出す、文書化およびテスト済みの方法、または制限についての明確に範囲を定めた説明です。

索引モデルが issue の本文から書いたものです。

説明

This issue is a (choose one):

  • Problem/bug report.

Checklist before submitting:

  • I've searched for an existing issue.

Description

Bug reports:

If for example I have such setup, I have Document model that have many DocumentsDocumentables, which have documentable as polymorphic association and can have either Employee or Comment in it. I set up :

class Document < ApplicationRecord
  has_many :documents_documentables, dependent: :destroy
end

class DocumentsDocumentable < ApplicationRecord
  belongs_to :document
  belongs_to :documentable, polymorphic: true
end

class Employee < ApplicationRecord
  has_many :documents_documentables, as: :documentable
end

class Comment < ApplicationRecord
  has_many :documents_documentables, as: :documentable
end

module Api
  module V1
    class DocumentResource < BaseResource
      attributes :file_name

      has_many :documents_documentables

      filter :id
    end
  end
end

module Api
  module V1
    class DocumentsDocumentableResource < BaseResource
      attributes :documentable_id, :documentable_type, :document_id

      has_one :document
      has_one :documentable, polymorphic: true, eager_load_on_include: false
    end
  end
end

module Api
  module V1
    class EmployeeResource < BaseResource
      filter :custom_employee_filter, apply: lambda { |records, value, _options|
                                  puts "custom employee filter"
                                }
    end
  end
end

module Api
  module V1
    class CommentResource < BaseResource
      filter :custom_comment_filter, apply: lambda { |records, value, _options|
                                  puts "custom comment filter"
                                }
    end
  end
end

Having this setup, how can I filter DocumentDocumentable by documentable.custom_<employee | comment>_filter then?

I tried smth like that:

/api/v1/documents-documentables?filter=documentable.customEmployeeFilter=filterValue , or
/api/v1/documents-documentables?filter=documentable.customCommentFilter=filterValue

Both gives me error:

{
  error: {
    code: "102",
    detail: "documentable.customEmployeeFilter is not allowed.",
    status: "400",
    title: "Filter not allowed"
  }
}

Is it possible at all somehow to call both filters, and if yes - how should I change my resources, is there any param I can pass to further specialize polymorpthic resource's type?

主要言語
Ruby
スター
2.3k
フォーク
546
PR マージ指標
30日以内にマージされた PR はありません

コントリビューションガイド

このリポジトリのコントリビューションガイドは索引されていません

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

JSONAPI-Resources/jsonapi-resources のほかの issue

JSONAPI-Resources/jsonapi-resources の issue をすべて見る

似ている issue

Ruby の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。