【问题标题】:How can show has_many :through Association with ActiveModel::Serializer如何显示 has_many :through 与 ActiveModel::Serializer 的关联
【发布时间】:2018-10-07 17:13:18
【问题描述】:

projectsproject_categoryproject_by_category 之间存在多对多关系。 我的模型:

Project Model

class Project < ApplicationRecord
  # Relationships
  has_many :project_by_categories
  has_many :project_categories, through: :project_by_categories

ProjectCategory Model

class ProjectCategory < ApplicationRecord
  has_many :project_by_categories
  has_many :projects, through: :project_by_categories

ProjectByCategory Model

class ProjectByCategory < ApplicationRecord
  # Relationships
  belongs_to :project
  belongs_to :project_category

我的序列化器:

Project Serializer

class ProjectSerializer < ActiveModel::Serializer
  attributes :id, :image, :name, :description

Project Category Serializer

  class ProjectCategorySerializer < ActiveModel::Serializer              
    attributes :id, :name

    has_many :projects

The expected result:

{
    "data": [
        {
            "id": "1",
            "type": "project-categories",
            "attributes": {
                "name": "3D design basics"
            },
            "relationships": {
                "projects": {
                    "data": [
                        {
                            "id": "1",
                             "image": "",
                             "name": "",
                             "description": "", 
                            "type": "projects"
                        },
                        {
                            "id": "2",
                             "image": "",
                             "name": "",
                             "description": "", 
                            "type": "projects"
                        }
                    ]
                }
            }
        },

But this is the result:

{
    "data": [
        {
            "id": "1",
            "type": "project-categories",
            "attributes": {
                "name": "3D design basics"
            },
            "relationships": {
                "projects": {
                    "data": [
                        {
                            "id": "1",
                            "type": "projects"
                        },
                        {
                            "id": "2",
                            "type": "projects"
                        }
                    ]
                }
            }
        },

在项目关系中只显示 id 和类型。

By last this is my controller

class Api::V1::CategoriesController < ApiController
    def index
        @categories = ProjectCategory.all
        render json: @categories
    end

感谢您的回答!!

【问题讨论】:

    标签: ruby-on-rails ruby activerecord activemodel active-model-serializers


    【解决方案1】:

    您的连接表似乎设置正确(并且您似乎从数据库中获取了预期的对象),但您只是缺少额外的序列化属性。

    您是否尝试将serializer:each_serializer: 参数传递给相应的序列化程序和控制器?例如,您的 Api::V1::CategoriesController 可能如下所示:

    class Api::V1::CategoriesController < ApiController
      def index
        @categories = ProjectCategory.all
        render json: @categories, each_serializer: ProjectCategorySerializer
      end
    end
    

    同样,您的ProjectCategorySerializer 将包含:

    class ProjectCategorySerializer < ActiveModel::Serializer
      attributes :id, :name
    
      has_many :projects, each_serializer: ProjectSerializer
    

    我不确定 ActiveModel::Serializers 现在是否是 ActiveSupport 的一部分,但the library 似乎表明开发已经逐渐减少。如果可能的话,我建议切换一个不同的库。

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-28
      • 2021-10-09
      • 1970-01-01
      相关资源
      最近更新 更多