Reputation: 595
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.
Upvotes: 2
Views: 1473
Reputation: 2541
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
Upvotes: 1