1

Currently I'm building an API where I have a model functioning as a category. The category has possible subcategories and/or a single parent category created using the acts_as_tree gem. I wish to serialize the category model and it's relations to itself using active_model_serializers gem.

Note: active_model_serializer uses the :json_api adapter

class NutritionCategory < ActiveRecord::Base
    has_many :nutritions
    acts_as_tree
end


class NutritionCategorySerializer < ActiveModel::Serializer
  attributes :id, :name, :description

  has_many :nutritions, embed: :ids
end

Since acts_as_tree does all the 'magic' I can't seem to find a way to serialize this relationship properly. How do I define the relationship in the serializer?

I'm using Ruby 2.2.1 with the following gems:

  • Rails (4.2.1)
  • acts_as_tree (2.1.0)
  • active_model_serializers (0.10.0.pre)
Daniël de Wit
  • 2,206
  • 1
  • 16
  • 13

1 Answers1

0

Try adding to your serializer:

has_many :children, embed: :ids