2

I am working on an RoR project with ruby-2.5.0 and rails 5. I am using the jsonapi-serializers for my API. I want to customize the attributes of the associated model. I have two models Receipt and ReceiptPartial. receipt has_many :receipt_partials. when I write has_many :receipt_partials in my serializer it returns all the columns but I want only a few columns.

class ReceiptPartialSerializer
  include JSONAPI::Serializer

  TYPE = 'receipt'
  attribute :id
  has_many :receipt_partials
end

I want to restrict the columns of receipt_partials. I also tried has_many :receipt_partials, only: ['id'] but didnot work. How can I achieve this? Please help. Thanks in advance.

1 Answer 1

1

Based on the documentation, you should simply specify the attributes on the relevant serializer class for example if you want to display id, and name attributes in ReceiptPartial Serializer you could do the following.

class BaseSerializer
  include JSONAPI::Serializer
end

class ReceiptSerializer < BaseSerializer
  TYPE = 'receipt'
  attribute :id

  has_many : receipt_partials
end

class ReceiptPartialSerializer < BaseSerializer
  attributes :id, :name
end
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.