How to Customize Rails API Output

Gigih JR
Binar Academy
Published in
3 min readNov 30, 2018

Terlintas pikiran seperti judul dalam artikel ini ketika saya belajar membuat sebuah Rails API. Apakah bisa? Bagaimana caranya? Setelah bertanya-tanya kepada Google dan rekomendasi dari seorang teman, akhirnya saya mendapat sebuah informasi bahwa ada sebuah gem yang bisa digunakan untuk melakukan hal tersebut. Dalam artikel ini, saya ingin berbagi pengalaman yang saya alami. Berikut langkah-langkah untuk melakukan hal tersebut.

Instalation

Pertama-tama kita buat sebuah Rails API project baru

$ rails new book-list --api -T --database=postgresql

Kemudian masuk ke directory project dan buat databasenya

$ rails db:create

Buka Gemfile dan tambahkan gem serializer

$ gem 'active_model_serializers'

Kemudian, install dependensi yang ditentukan dalam Gemfile

$ bundle install

Cara termudah untuk membuat serializer ketika kita membuat sebuah model adalah sebagai berikut:

$ rails g resource author name
$ rails g resource book title author_id:integer published_at:datetime

Migrasikan tabel yang telah dibuat ke dalam database

$ rails db:migrate

Buka file model yang telah dibuat dan tambahkan relationship kedalamnya

# models ######################################class Author < ApplicationRecord
has_many :books, dependent: :destroy
end
class Book < ApplicationRecord
belongs_to :author
end

Buka file controller dan tambahkan action ke dalamnya

#controllers ##################################class AuthorsController < ApplicationController
def index
render json: Author.all
end
def show
render json: Author.find(params[:id])
end
end
class BooksController < ApplicationController
def index
render json: Book.all
end

def show
render json: Book.find(params[:id])
end
end

Masukkan dummy data ke dalam database melalui rails console

# dummy data ###################################
rowling = Author.create({name: “J. K. Rowling”})
aoyama = Author.create({name: “Gosho Aoyama”})
rowling.books.create({title: “Fantastic Beast”, published_at: “2003–03–14 13:04:44”})
rowling.books.create({title: “Harry Potter”, published_at: “2005–06–15 15:05:55”})
rowling.books.create({title: “The Casual Vacancy”, published_at: “2009–01–11 08:44:13”})
aoyama.books.create({title: “Magic Kaito”, published_at: “2005–03–14 13:04:44”})
aoyama.books.create({title: “Yaiba”, published_at: “2007–11–07 09:07:32”})
aoyama.books.create({title: “Detective Conan”, published_at: “2009–05–03 11:07:57”})

Rails API output

authors output
books output

add relationship into serializer

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

Results authors

[
{
“id”: 1,
“name”: “J. K. Rowling”,
“books”: [
{
“id”: 1,
“title”: “Fantastic Beast”,
“published_at”: “2003–03–14T13:04:44.000Z”
},
{
“id”: 2,
“title”: “Harry Potter”,
“published_at”: “2005–06–15T15:05:55.000Z”
},
{
“id”: 3,
“title”: “The Casual Vacancy”,
“published_at”: “2009–01–11T08:44:13.000Z”
}
]
},
{
“id”: 2,
“name”: “Gosho Aoyama”,
“books”: [
{
“id”: 4,
“title”: “Magic Kaito”,
“published_at”: “2005–03–14T13:04:44.000Z”
},
{
“id”: 5,
“title”: “Yaiba”,
“published_at”: “2007–11–07T09:07:32.000Z”
},
{
“id”: 6,
“title”: “Detective Conan”,
“published_at”: “2009–05–03T11:07:57.000Z”
}
]
}
]
class BookSerializer < ActiveModel::Serializer
attributes :id, :title, :published_at
belongs_to :author
end

Results books

[
{
“id”: 1,
“title”: “Fantastic Beast”,
“published_at”: “2003–03–14T13:04:44.000Z”,
“author”: {
“id”: 1,
“name”: “J. K. Rowling”
}
},
{
“id”: 2,
“title”: “Harry Potter”,
“published_at”: “2005–06–15T15:05:55.000Z”,
“author”: {
“id”: 1,
“name”: “J. K. Rowling”
}
},
{
“id”: 3,
“title”: “The Casual Vacancy”,
“published_at”: “2009–01–11T08:44:13.000Z”,
“author”: {
“id”: 1,
“name”: “J. K. Rowling”
}
},
{
“id”: 4,
“title”: “Magic Kaito”,
“published_at”: “2005–03–14T13:04:44.000Z”,
“author”: {
“id”: 2,
“name”: “Gosho Aoyama”
}
},
{
“id”: 5,
“title”: “Yaiba”,
“published_at”: “2007–11–07T09:07:32.000Z”,
“author”: {
“id”: 2,
“name”: “Gosho Aoyama”
}
},
{
“id”: 6,
“title”: “Detective Conan”,
“published_at”: “2009–05–03T11:07:57.000Z”,
“author”: {
“id”: 2,
“name”: “Gosho Aoyama”
}
}
]

Kesimpulan

Dengan menggunakan gem Active Model Serializer kita dapat memodifikasi Rails API output baik menambahkan atau mengurangi element output.

Mohon maaf apabila terdapat kekurangan dan kesalahan dalam artikel ini karena penulis masih belajar.

sumber: https://github.com/rails-api/active_model_serializers

--

--