【发布时间】:2019-04-16 16:35:55
【问题描述】:
我创建了一个 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
【问题讨论】:
标签: ruby-on-rails ruby ruby-on-rails-5 fastjsonapi