tiagopog/jsonapi-utils

Error 500 when request wants to sort by non database resident attribute

bananartist opened this issue · 2 comments

To clarify:

# app/resources/user_resource.rb
class UserResource < JSONAPI::Resource
  attributes :first_name, :last_name, :full_name, :email, :image_url, :is_admin                                                                                                                                    

  has_many :projects

  def full_name
    @model.name
  end
end
# app/models/user_model.rb
...
  # Convenient way of getting full name                                                                                                                                                                            
  def name
    [first_name, last_name].join(' ')                                                                                                                                                                              
  end

The request was : http://localhost:3000/users?sort=fullName

Found the solution in JSONAPI::Resources Gem:

  def self.sortable_fields(context)
    super(context) - [:full_name]
  end

Sorry to have bothered you !

👍