class CreditCardResource < ApplicationResource
self.polymorphic = %w(VisaResource MastercardResource)
# everybody gets this
attribute :number, :integer
end
class VisaResource < CreditCardResource
attribute :custom, :string do
'custom rendered attribute just for visa'
end
# This can be sideloaded via CreditCardResource
has_many :rewards
end
class MastercardResource < CreditCardResource
attribute :number, :integer do
'override'
end
end
In other words: query through the parent, then use the children for
serialization and sideloading. The above would correctly handle
CreditCard.all returning an array of Visa and MasterCard
instances, rendering different jsonapi types and only serializing
relationships when relevant.
When we say "polymorphic", think "Rails STI":
In other words: query through the parent, then use the children for serialization and sideloading. The above would correctly handle
CreditCard.all
returning an array ofVisa
andMasterCard
instances, rendering different jsonapitype
s and only serializingrelationships
when relevant.