JSONAPI-Resources / JSONAPI-Resources/jsonapi-resources

Filter included resources or way around it

Open
#890 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Ruby
Stars
2.3k
Forks
546
PR merge metrics
No merged PRs in 30d

Description

Hi,

Is there currently a way of filtering resources that are included to our response?

ie: I want to get and include books with that response I'll just do

address/shelves?include=books

but what if I would like to filter by only available books?

ie: address/shelves/?include=books&filter[books.available]=true

books.available is just a scope in this case

or is the preffered method to do this by declaring additional relationships in the shelf resource?
like below

class ShelfResource < JSONAPI::Resource
  has_many :books
  has_many :available_books
  ...
end

If the latter, how do I exactly specify the method for finding resources for this relationship?

I've tried that already by adding

def records_for_available_books
  _model.books.available
end

to the shelf resource but I keep getting errors when doing that.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with ShelfResource's has_many :books relationship and the records_for_available_books method, then trace how include and filter[books.available] are handled. Reproduce the reported errors and compare the relationship declaration with the requested filtered response. Done means the supported way to filter included books is established and the example works, or the limitation and preferred relationship approach are documented.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
api, backend
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.