active_model_serializers icon indicating copy to clipboard operation
active_model_serializers copied to clipboard

Enumeration

Open exocode opened this issue 4 years ago • 0 comments
trafficstars

Expected behavior vs actual behavior

How to iterate through an array with relations? And how to pass through "columns"-method (example below) one serializer deeper?

ANY help appreciated (thank you)

Steps to reproduce

class Category
  has_many :categories # self referencing
  attribute_reader :name
  #  nested arrays with Categories grouped in chunks of pieces
  def columns # how can I use a serializer on these array of arrays. Somehow this method is not able to serialize. The only way it works is the 'approach_one', but it will respond with ALL available attributes, where I only need name (and some others
     [
        [Category.find(1), Category.find(2), Category.find(2)]
        [Category.find(4), Category.find(5), Category.find(6)]
     ]
  end
end

class ColumnSerializer 
  attribute :grouped_children, key: :approach_one, serializer: CategoryMenuColumnsSerializer # outputs everything of "colums"-method. But does not use Serializer in any way
  # another approach
  attribute :grouped_children do |serializer|
    serializer.grouped_children.each do |column|
      CategoryMenuColumnsSerializer.new(column)
    end
  end
end

class CategoryMenuColumnsSerializer < ActiveModel::Serializer
  attribute :name, key: :title
end

This is what I get:

            "menu": {
                 "columns": [
                    [
                        {
                            "id": 1,
                            "name": "Baby Badebedarf",
                            "created_at": "2021-04-04T17:03:25.976Z",
                            "updated_at": "2021-04-04T17:03:25.995Z",
                            "ancestry": "537",
                            "ancestry_depth": 1,
                            "gid": null,
                            "slug": "baby-badebedarf",
                            "children_count": 2,
                            "products_count": 0,
                            "main_products_count": 0
                        },
                        {
                            "id": 2,
                            "name": "Babytransport",
                            "created_at": "2021-04-04T17:03:26.193Z",
                            "updated_at": "2021-04-04T17:03:26.221Z",
                            "ancestry": "537",
                            "ancestry_depth": 1,
                            "gid": null,
                            "slug": "babytransport",
                            "children_count": 3,
                            "products_count": 0,
                            "main_products_count": 0
                        }
                    ],

Expected result

            menu: {
                columns: [                # MUST be an Array attribute gives me only Hash (curly braces), 
                    {
                        size: 3,
                        links: [             # Links must be an Array of Hashes
                            {
                                title: 'Power Tools',
                                url: '',
                                children: [
                                    { title: 'Engravers', url: '' },
                                    { title: 'Drills', url: '' },
                                    { title: 'Wrenches', url: '' },
                                ],
                            },
                            { title: 'Workbenches', url: '' },
                            { title: 'Presses', url: '' },
                        ],
                    },
                    {
                        size: 3,
                        links: [
                            {
                                title: 'Hand Tools',
                                url: '',
                                children: [
                                    { title: 'Screwdrivers', url: '' },
                                    { title: 'Handsaws', url: '' },
         
                                ],
                            },
                            {
                                title: 'Garden Equipment',
                                url: '',
                                children: [
                                    { title: 'Motor Pumps', url: '' },
                                    { title: 'Chainsaws', url: '' },
                                ],
                            },
                        ],
                    },
               ]
         }
    }

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)

exocode avatar Apr 15 '21 17:04 exocode