如何显示has_many:通过与ActiveModel :: Serializer的关联

时间:2018-04-26 22:11:04

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

我在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

感谢您的回答!!

1 个答案:

答案 0 :(得分:0)

您的联接表似乎已正确设置(您似乎正在从数据库中获取预期的对象),但您只是缺少其他序列化属性。

您是否尝试将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似乎表明开发已逐渐消失。如果可能,我建议切换一个不同的库。

希望这有帮助!