gpt4 book ai didi

ruby-on-rails - 如何使用 rails 中的序列化程序将深层嵌套的关联渲染到 json?

转载 作者:行者123 更新时间:2023-12-05 06:19:14 24 4
gpt4 key购买 nike

我只是在做一个简单的 Rails 项目,其中模型之间有很多关系:

  1. 一个作者可以有多个帖子
  2. 一个帖子可以有很多评论
  3. 喜欢和不喜欢属于每个帖子

现在,我已经渲染了作者数据(在 json 中),我得到的输出是这样的:

enter image description here

我们可以说它只呈现作者和帖子数据(既不评论也不喜欢/不喜欢)。

我是 RubyOnRails 的新手。所以,到目前为止,无论我尝试过什么,都在下面:

Controller :

class AuthorsController < ApplicationController
def show
@auth = Author.find_by(id: params[:id])
render json: @auth
end
end

模型:

class Author < ApplicationRecord
has_many :posts
end

class Comment < ApplicationRecord
belongs_to: post
end

class Dislike < ApplicationRecord
belongs_to: post
end

class Like < ApplicationRecord
belongs_to: post
end

class Post < ApplicationRecord
has_many :comments
end

序列化器:

class AuthorSerializer < ActiveModel::Serializer
attributes :id, :name, :age
has_many :posts
end

class CommentSerializer < ActiveModel::Serializer
attributes :id, :content, :username
end

class DislikeSerializer < ActiveModel::Serializer
attributes :id, :dislikecount
end

class LikeSerializer < ActiveModel::Serializer
attributes :id, :likecount
end

class PostSerializer < ActiveModel::Serializer
attributes :name, :content
has_many :comments, serializer: CommentSerializer
end

架构.rb:

ActiveRecord::Schema.define(version: 2020_03_25_091544) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

create_table "authors", force: :cascade do |t|
t.string "name"
t.integer "age"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end

create_table "comments", force: :cascade do |t|
t.string "content"
t.string "username"
t.bigint "post_id"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["post_id"], name: "index_comments_on_post_id"
end

create_table "dislikes", force: :cascade do |t|
t.integer "dislikecount"
t.bigint "post_id"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["post_id"], name: "index_dislikes_on_post_id"
end

create_table "likes", force: :cascade do |t|
t.integer "likecount"
t.bigint "post_id"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["post_id"], name: "index_likes_on_post_id"
end

create_table "posts", force: :cascade do |t|
t.string "name"
t.string "content"
t.bigint "author_id"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["author_id"], name: "index_posts_on_author_id"
end

end

现在,我只想以 json 形式呈现作者的完整数据(意味着,预期输出必须包括作者详细信息+帖子详细信息+评论+喜欢+不喜欢)。

我已经进行了很多搜索来解决这个问题,但无法解决这个问题。

最佳答案

您需要对代码进行 2 处更改 -

class Post < ApplicationRecord
has_many :comments
has_many :likes
has_many :dislikes
belongs_to :author
end

class PostSerializer < ActiveModel::Serializer
attributes :name, :content
has_many :comments, serializer: CommentSerializer
has_many :likes
has_many :dislikes
end

关于ruby-on-rails - 如何使用 rails 中的序列化程序将深层嵌套的关联渲染到 json?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60863842/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com