Make Rails Through Associations Render UNDER what They're Through

Viewed 26

Ok, so I have this in my serializer and model:

class Review< ApplicationRecord
    has_one :gamesection
        has_many :releases, through: :gamesection
        has_one :cover, through: :gamesection
        has_many :otherpics, through: :cover, through: :gamesection

But the problem is that it renders like

Review
->gamesection
->release
->cover
->otherpics

While I want it to be

Review
->gamesection
--->release
--->cover
--->otherpics

How can I accomplish that?

1 Answers

You should have below in your rails serializer.

# /app/serializers/review_serializer.rb
class ReviewSerializer < ActiveModel::Serializer
  attributes :id,
             :title,
  has_one :gamesection
end

# /app/serializers/gamesection_serializer.rb
class GamesectionSerializer < ActiveModel::Serializer
  attributes :id,
            
  has_many :releases
  has_one  :cover
  has_many :otherpics 
end
Related