首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何用fast_jsonapi序列化Rails中的habtm关系

如何用fast_jsonapi序列化Rails中的habtm关系
EN

Stack Overflow用户
提问于 2018-11-13 19:03:40
回答 2查看 397关注 0票数 0

我创建了一个Rails应用程序,我想用佐纳皮实现一个JSON。现在我在为那些没有显示出来的关系而挣扎。我要换什么?

这是我的schema.db:

代码语言:javascript
复制
create_table "candidates", force: :cascade do |t|
  t.string "place"
  t.string "zip_code"
  t.string "address"
  t.string "date_of_birth"
  t.string "title"
  t.string "profile_picture"
  t.string "first_name"
  t.string "last_name"
  t.string "email_address"
  t.boolean "confirm_terms_and_conditions"
  t.datetime "created_at", null: false
  t.datetime "updated_at", null: false
end

create_table "candidates_degrees", id: false, force: :cascade do |t|
  t.bigint "candidate_id"
  t.bigint "degree_id"
  t.index ["candidate_id"], name: "index_candidates_degrees_on_candidate_id"
  t.index ["degree_id"], name: "index_candidates_degrees_on_degree_id"
end

create_table "degrees", force: :cascade do |t|
  t.string "degree"
  t.datetime "created_at", null: false
  t.datetime "updated_at", null: false
end

这些是我的模型:

代码语言:javascript
复制
class Candidate < ApplicationRecord
  has_and_belongs_to_many :degrees, dependent: :nullify
end

class Degree < ApplicationRecord
  has_and_belongs_to_many :candidates, dependent: :nullify
end

这些是我的序列化程序:

代码语言:javascript
复制
class CandidateSerializer
  include FastJsonapi::ObjectSerializer
  attributes :place, :zip_code, ...
  has_many :degrees
end

class DegreeSerializer
  include FastJsonapi::ObjectSerializer
  attributes :degree
  has_many :candidates
end
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-11-14 10:54:08

您需要对CandidateSerializerDegreeSerializer进行更改。

与在序列化器中写入单独的HABTM关系不同,您可以直接在attributes中编写

e.g

代码语言:javascript
复制
class CandidateSerializer
  include FastJsonapi::ObjectSerializer
  attributes :place, :zip_code,:degrees
end

响应

代码语言:javascript
复制
{
  :data=>
    {:id=>"",
     :type=>:candidate,
     :attributes=> {   
       degrees: {}
     }
 }

同样适用于DegreeSerializer

代码语言:javascript
复制
  class DegreeSerializer
    include FastJsonapi::ObjectSerializer
    attributes :candidates
  end
票数 0
EN

Stack Overflow用户

发布于 2020-10-06 19:49:42

你可以这样做:

代码语言:javascript
复制
class CandidateSerializer
  include FastJsonapi::ObjectSerializer
  attributes :place, :zip_code,...
  has_many :degrees, if: Proc.new { |record| record.association(:dregrees).loaded? }
end
代码语言:javascript
复制
class DegreeSerializer
  include FastJsonapi::ObjectSerializer
  has_many :candidates, if: Proc.new { |record| record.association(:candidates).loaded? }
end

在您的API操作中(例如,在这里使用显示路由):

代码语言:javascript
复制
def show
  @candidate.degrees.load
  render json: CandidateSerializer.new(@candidate, options).serialized_json if stale?(@candidate)
end

private

def set_candidate
  @candidate = Candidate.find_by(id: params[:id])
end

def options
  { include: [:degrees] }
end

结果

代码语言:javascript
复制
{
    "data": {
        "id": "20",
        "type": "candidate",
        "attributes": {
            "id": 20,
            ...
        },
        "relationships": {
            "degrees": {
                "data": [
                    {
                        "id": "713",
                        "type": "degree"
                    }
                ]
            }
        }
    },
    "included": [
        {
            "id": "713",
            "type": "degree",
            "attributes": {
                "id": 713,
                ...
            },
            "relationships": {}
        }
    ]
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53287872

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档