I have a couple models. Let's call them Widget and Gadget.
My #index for for Widget and Gadget looks something like this
def index
widgets = Widget.all
if widgets
respond_with widgets, each_serializer: Api::V1::WidgetSerializer
else
render json: { message: [t(:not_found_widget)] }, status: :not_found
end
end
def index
gadgets = Gadget.all
if gadgets
respond_with gadgets, each_serializer: Api::V1::GadgetSerializer
else
render json: { message: [t(:not_found_gadget)] }, status: :not_found
end
end
And my serializers...
class Api::V1::WidgetSerializer < ActiveModel::Serializer
attributes :id, :desc
end
class Api::V1::GadgetSerializer < ActiveModel::Serializer
attributes :id, :desc
end
However, I have the need for a resource that returns both of those in 1. I need both widgets and gadgets returned at once. So the json would look like...
{
"widgets": [
{
"id": 1,
"desc": "One"
},
{
"id": 2,
"desc": "Two"
}
],
"gadgets": [
{
"id": 1,
"desc": "One"
},
{
"id": 2,
"desc": "Two"
}
]
}
How can I achieve this. Something like
widgets = Widget.all
gadgets = Gadget.all
respond_with widgets, each_serializer: Api::V1::WidgetSerializer, gadgets, each_serializer: Api::V1::GadgetSerializer
However, this clearly doesn't work.