How to filter by polymorphic resource association filter?
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 25/100
Research direction
Start with the DocumentsDocumentableResource declaration of the polymorphic documentable association and the two filter request examples. Trace how JSONAPI Resources validates filters for polymorphic relationships and determine whether type-specific dispatch is supported. Done means a documented, tested way to invoke the appropriate filter, or a clearly scoped explanation of the limitation.
Written by the indexing model from the issue text.
Description
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?
- Dominant language
- Ruby
- Stars
- 2.3k
- Forks
- 546
- PR merge metrics
- No merged PRs in 30d
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from JSONAPI-Resources/jsonapi-resources
-
Difficulty 1/5 Under an hour Newbie friendliness 85/100
-
Difficulty 3/5 1-2 days Newbie friendliness 50/100
JSONAPI-Resources/jsonapi-resources#1488 · 1 comment ·
-
Difficulty 4/5 3-5 days Newbie friendliness 42/100
JSONAPI-Resources/jsonapi-resources#1479 · 2 comments ·
-
Difficulty 4/5 3-5 days Newbie friendliness 30/100
JSONAPI-Resources/jsonapi-resources#1477 · 3 comments · 1 reaction ·
-
Difficulty 3/5 1-2 days Newbie friendliness 45/100
All issues in JSONAPI-Resources/jsonapi-resources
Similar issues
-
Difficulty 1/5 Under an hour Newbie friendliness 90/100
TheOdinProject/curriculum#31423 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
oxidize-rb/rb-sys#807 ·
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
-
ai-generated
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 75/100