JSONAPI-Resources / JSONAPI-Resources/jsonapi-resources

Error with default sorting on relationship field

Open
#1,015 1 comment 1 reaction 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

Hello!
I'm using version 0.8.1 (also Rails 4.2.5.1 and Ruby 2.1.10) and following code

class A < Resource
    attributes :name
    has_one :b

    def self.default_sort
      [{field: 'b.name', direction: :asc}, {field: 'name', direction: :asc}]
    end 
end

class B < Resource
    attributes :name
end

results with following error, when I'm trying to make any request to A's resource.

ERROR -- : Internal Server Error: undefined method `model' for #<Class:0x000000042533a8> /vendor/bundle/ruby/2.1.0/gems/activerecord-4.2.5.1/lib/active_record/dynamic_matchers.rb:26

It is happening in apply_sort method, when try to get records.model.to_s:

associations = _lookup_association_chain([records.model.to_s, *model_names])

Is it a bug or something is wrong with my code?

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

Reproduce the request with the A and B resources, Rails 4.2.5.1, Ruby 2.1.10, and jsonapi-resources 0.8.1. Inspect apply_sort and the _lookup_association_chain call around records.model to determine whether relationship-field sorting is supported; done means the request no longer raises the reported undefined-method error or the limitation is clearly established.

Written by the indexing model from the issue text.

Assessment

Tech stack
rails, ruby
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.