Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to nest serializers? #2417

Open
exocode opened this issue Apr 12, 2021 · 2 comments
Open

How to nest serializers? #2417

exocode opened this issue Apr 12, 2021 · 2 comments

Comments

@exocode
Copy link

exocode commented Apr 12, 2021

Expected behavior vs actual behavior

Serialize nested attributes (and exclude some attributes from rendering)
I can see the children in json output but the nested Serializer is not

Steps to reproduce

install gem ancestry
for polymorphic / self referencing models

model:

class Category
  has_ancestry cache_depth: true, touch: true, counter_cache: true
  attribute :name
  attribute :attribute_to_exclude_from_children_only
end

create some Categories

root = Category.create!(name: "Root")
child = root.children.create!(name: "child1")
baby = child.children.create!(name: "baby")

controller:

class CategoryController < ApplicationController
  def main_menu
      # take one of the following two lines
      roots = Category.roots 
      roots = Category.roots.first.subtree

      render json: roots , each_serializer: MenuSerializer # , include: children <- won't work too
  end
end

Serializers:

class MenuSerializer
     attribute(:name)
     attribute :attribute_to_exclude_from_children_only
     attribute(:children), each_serializer: ChildSerializer
end
class ChildSerializer
   # here only "name" should be rendered, but ALL attributes (created_at, attribute_to_exclude_from_children_only,..) are rendered too... 
     attribute(:name) # I set here a breakpoint, which was called once, but seems not kick in to render properly
     attribute(:children), each_serializer: ChildSerializer
end

what I also tried (maybe some weired things too, but I am quite desperate

  has_many :children, key: :aaa do |child|
    ChildSerializer.new(child)
  end
  has_many :children, key: :bbb, each_serializer: ChildSerializer

  attribute :children, key: :ccc, each_serializer: ChildSerializer
  attribute(:children, key: :ddd, each_serializer: ChildSerializer, include: :children, polymorphic: true)
  class ChildSerializerTest < ActiveModel::Serializer
    attribute :name
  end

Only :ccc and :ddd gets rendered, but with ALL attributes (created_at,...)

Can someone help me to get this going? Thank you VERY MUCH in advance

Environment

ActiveModelSerializers Version (commit ref if not on tag):

Output of ruby -e "puts RUBY_DESCRIPTION":
ruby 2.7.2p137 (2020-10-01 revision 5445e04352) [x86_64-darwin20]
OS Type & Version:
Darwin 20.3.0 Darwin Kernel Version 20.3.0: Thu Jan 21 00:07:06 PST 2021; root:xnu-7195.81.3~1/RELEASE_X86_64 x86_64
Integrated application and version (e.g., Rails, Grape, etc):
Rails "6.1.3.1"

Backtrace

(e.g., provide any applicable backtraces from your application)

Additonal helpful information

(e.g., Gemfile.lock, configurations, PR containing a failing test, git bisect results)

gem 'active_model_serializers', '~> 0.10.0'
  active_model_serializers (~> 0.10.0)
      active_model_serializers (0.10.12)
@bf4
Copy link
Member

bf4 commented Apr 13, 2021

Which adapter are your using?

So a category should be serialized by a menu serializer?
And the category's children are not relations?
How many levels deep are you trying to serialize?

@exocode
Copy link
Author

exocode commented Apr 13, 2021

Which adapter are your using?

Default one (not JsonAPI, or something else)

ActiveRecord PostgreSQL

So a category should be serialized by a menu serializer?

Yes, but only the first category in the chain,... all subcategories should take an other serializer

And the category's children are not relations?

Not real ones, they are methods created by 'ancestry' gem

How many levels deep are you trying to serialize?

Maximum 8

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants