0

I inherited a JSONAPI::Processor from 2017 (now is Jul/2023):

module Api
 module V2
   class GivingNumberSearchProcessor < JSONAPI::Processor
     def create_resource
      data = params[:data]
       search = GivingNumberSearch.new(
         country: data[:attributes][:country],
         area_code: data[:attributes][:area_code]
      )

       search.execute
       resource = GivingNumberSearchResource.new(search, context)
      JSONAPI::ResourceOperationResult.new(:created, resource)
    end
  end
 end
end

search.execute returns an array of ActiveRecord objects.

Apparently JSONAPI::ResourceOperationResult changed to JSONAPI::ResourceSetOperationResult. But after upgraded the name I'm getting:

"undefined method `resource_klasses' for #\u003cApi::V2::GivingNumberSearchResource
 "backtrace":["/home/xpsman/.rvm/gems/ruby-3.0.6/gems/jsonapi-resources-0.10.7/lib/jsonapi/resource_serializer.rb:51   

I found the create_resource method. It seems I'm missing a step to add the "resource_klasses" part before passing the value to the serializar, but is my first week with JSONAPI::Resources.

# file: app/resources/api/v2/giving_number_search_resource.rb    
module Api
  module V2
    class GivingNumberSearchResource < JSONAPI::Resource

     attributes :area_code, :country
     has_many :inbound_numbers
   end
  end
end
aarkerio
  • 2,183
  • 2
  • 20
  • 34
  • Sure. Added the file. – aarkerio Jul 18 '23 at 19:21
  • I notice you are not utilizing the return value from `search.execute`. I am assuming that `GivingNumberSearch` is a service object (PORO) of some kind and thus does not play nice with jsonapi-resources. Is it possible that this is the actual issue, whereby you meant to do one of the following: `search = GivingNumberSearch.new(country: data[:attributes][:country],area_code: data[:attributes][:area_code]).execute` or `resource = GivingNumberSearchResource.new(search.execute, context)`? – engineersmnky Jul 18 '23 at 19:26

0 Answers0