5

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?

  • 1
    define "everything". To `include` related 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 asking – engineersmnky Dec 05 '16 at 21:23
  • @engineersmnky "Everything" is all the things listed in the serializer, as I stated. When i render multiple objects, I want it to return the attributes, belong_to's, has_many's, has_one's, etc. Its only generating the attributes when I try to do multiple objects –  Dec 05 '16 at 21:28
  • please provide example output as you'd like to see it. If you look at my example it will provide you with what you want but you have to ask for what you want by telling json to `include` it. – engineersmnky Dec 05 '16 at 21:32
  • When I call { render json: beers }, one of the beers I get back looks like this: { id: 1, name: "beer", relationships: { flavor: "blah" } }. But when I call { render json: { beers: beers, micros: micros } } the one of the bees I get back looks like this: { id: 1, name: "beer" }. In other words, there is no relationships. The relationships are my belongs_to, has_many, etc. calls in the serializer. Why when I send back multiple objects, do the relationships not get rendered? I dont know how much more clear of a question I can make @engineersmnky –  Dec 05 '16 at 21:36
  • I think this is because former uses the `ActiveModelSerializer` and the latter is serializing a `Hash` and uses the default `JSON` serialization pattern but without digging into all the source behind both them I cannot guarantee this. I would recommend `include` or redefining `as_json` to handle this for you. – engineersmnky Dec 05 '16 at 21:48
  • Okay, I tried the include part of the other pieces I want and it worked. You must be right about when rendering multiple objects, it doesn't use the Serializer. –  Dec 05 '16 at 21:50

2 Answers2

11

hope to help you

def index
  @beers = Beer.all
  @micros = Micros.all

  render json: {
     beers: ActiveModel::Serializer::CollectionSerializer.new(@beers, each_serializer: BeerSerializer),
     micros: ActiveModel::Serializer::CollectionSerializer.new(@micros, each_serializer: MicroSerializer),
  }
end
Hai Nguyen
  • 458
  • 9
  • 15
  • 1
    This works, but if you want to use `scope` to access the `current_user` object inside the serializer, scope comes in as nil. – Kaka Ruto Sep 01 '20 at 17:17
0

The following code snippet works for me, reference link https://github.com/rails-api/active_model_serializers/issues/1091#issuecomment-477015183

def index
  @beers = Beer.all
  @micros = Micros.all

  render json: {
     beers: ActiveModelSerializers::SerializableResource.new(@beers, each_serializer: BeerSerializer),
     micros: ActiveModelSerializers::SerializableResource.new(@micros, each_serializer: MicroSerializer),
  }
end

To check in console: just put .as_json

ActiveModelSerializers::SerializableResource.new(@beers, each_serializer: BeerSerializer).as_json
Kishan Ku. Patel
  • 311
  • 1
  • 2
  • 10