如何使用fast_jsonapi在Ruby on Rails中序列化habtm关系

时间:2018-11-13 19:03:40

标签: ruby-on-rails ruby ruby-on-rails-5 fastjsonapi

我创建了一个Ruby on Rails API应用程序,我想在其中使用fast_jsonapi实现JSON api。现在,我正在努力处理未显示的关系。我必须更改什么?

这是我的schema.db:

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

这些是我的模型:

class Candidate < ApplicationRecord
  has_and_belongs_to_many :degrees, dependent: :nullify
end

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

这些是我的序列化器:

class CandidateSerializer
  include FastJsonapi::ObjectSerializer
  attributes :place, :zip_code, ...
  has_many :degrees
end

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

2 个答案:

答案 0 :(得分:1)

您可以这样做:

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

并且在您的API动作中(例如此处的show route):

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

结果

{
    "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 :(得分:0)

您需要在CandidateSerializerDegreeSerializer中进行更改。

您可以直接在attributes

中,而不用在序列化器中编写单独的HABTM关系。

例如

class CandidateSerializer
  include FastJsonapi::ObjectSerializer
  attributes :place, :zip_code,:degrees
end

回复

{
  :data=>
    {:id=>"",
     :type=>:candidate,
     :attributes=> {   
       degrees: {}
     }
 }

DegreeSerializer相同

  class DegreeSerializer
    include FastJsonapi::ObjectSerializer
    attributes :candidates
  end