I am trying to return multiple objects with the call:
def index
beers = Beer.all
micros = Micros.all
render json: {beers: beers, micro: micros}
end
However, for both objects is only returning the attributes listed in the respected serializers, not any of the has_many, belongs_to, etc. relationships in the respected serializers.
If I am just trying to return one single object, such as:
def index
beers = Beer.all
render json: beers
end
Then it works fine and returns all the relationships listed in the serializer.
How do I fix the call with multiple objects to return everything in the serializer, not just the attributes?
includerelated models you have to tell the serializer to include them (e.g.render json: beers, include: [:some_associated_model, :another_associated_model]) If that is the question you are askingincludeit.ActiveModelSerializerand the latter is serializing aHashand uses the defaultJSONserialization pattern but without digging into all the source behind both them I cannot guarantee this. I would recommendincludeor redefiningas_jsonto handle this for you.