Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

How to filter by polymorphic resource association filter?

Đang mở
#1,324 0 bình luận 1 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
5/5
Thời gian dự kiến
Hơn một tuần
Mức phù hợp với người mới
25/100
Loại issue
Lỗi
Độ rõ ràng
Cần làm rõ
Mức độ hoạt động
Đình trệ
Công nghệ
rails, ruby
Lĩnh vực
api, backend

Hướng nghiên cứu

Bắt đầu với khai báo liên kết documentable đa hình của DocumentsDocumentableResource và hai ví dụ về yêu cầu bộ lọc. Truy vết cách JSONAPI Resources xác thực các bộ lọc cho các quan hệ đa hình và xác định xem việc dispatch theo từng type có được hỗ trợ hay không. Được xem là hoàn thành khi có một cách được lập tài liệu và kiểm thử để gọi bộ lọc phù hợp, hoặc một giải thích có phạm vi rõ ràng về hạn chế này.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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?

Ngôn ngữ chính
Ruby
Star
2.3k
Fork
546
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Hướng dẫn đóng góp

Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của JSONAPI-Resources/jsonapi-resources

Tất cả issue của JSONAPI-Resources/jsonapi-resources

Issue tương tự

Thêm issue về Ruby

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.