Vishal Srivastav
Vishal Srivastav

Reputation: 603

How to render deeply nested associations to json using serializer in rails?

I am just working on a simple rails project in which the models have these many relationships between them :

  1. A author can have many posts
  2. A post can have many comments
  3. likes and dislikes belongs to each post

Now, I have rendered the authors data ( in json ) and the output which I am getting is this :

enter image description here

As we can say that it is rendering only author and post data ( neither comments nor likes/dislikes ).

I am very new to RubyOnRails. So, Whatever I have tried so far is this below :

Controller :

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

Models :

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

Serializers :

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

schema.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

Now, I just want to render complete data of a author ( Means, The expected output must include author detail + post details + comments + likes + dislikes ) in json form.

I have searched a lot to overcome this issue, but could not resolve this issue.

Upvotes: 3

Views: 412

Answers (1)

Gautam
Gautam

Reputation: 1812

You need to make 2 changes in your code -

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

Upvotes: 1

Related Questions