0

I am using grape API and returning response using grape entity framework.

get '/' do
  users = User.all
  present users, with: API::Entities::UserInfo
end

module API
  module Entities
    class UserInfo < Grape::Entity
      expose 'UserInfo' do
        expose(:UserId) do |users, options|
          user.id
        end
        expose(:CompanyId) do |users, options|
          user.company.id
        end
      end
    end
  end
end

Expecting an output if users present. Array of active record relations(This works out)

 [{"UserInfo":{"UserId":4848,"CompanyId":276}},{"UserInfo":{"UserId":700,"CompanyId":276}}]

Expecting an output if users blank. Empty Array of active record relations (How to handle this)

 [{"UserInfo":{"UserId":null,"CompanyId":null}},{"UserInfo":{"UserId":null,"CompanyId":null}}]
Sam
  • 5,040
  • 12
  • 43
  • 95

0 Answers0