LoginSignup
11
13

More than 5 years have passed since last update.

Active Model Serializersを試してみる

Posted at

まずは今まで通りにjsonを出力

app/controllers/articles_controller.rb
class ArticlesController < ApplicationController
  def last
    render json: Article.last
  end
end
app/models/article.rb
class Article < ActiveRecord::Base
  belongs_to :category
end
{
    "id": 224,
    "category_id": 4,
    "title": "Active Model Serializersを試してみる",
    "thumbnail": "http://railscasts.com/static/episodes/stills/409-active-model-serializers.png",
    "created_at": "2014-05-13T08:40:24.000Z",
    "updated_at": "2014-05-13T08:40:24.000Z"
}

次はSerializersを利用
Gemfileにserializersを追記してbundle install

Gemfile
gem "active_model_serializers"

article_serializer.rbの追加

app/serializers/article_serializer.rb
class ArticleSerializer < ActiveModel::Serializer
  attributes :id, :title, :thumbnail
end

これだけ。jsonの確認

{
    "article": {
        "id": 224,
        "title": "Active Model Serializersを試してみる",
        "thumbnail": "http://railscasts.com/static/episodes/stills/409-active-model-serializers.png"
    }
}

コントローラーをちょっと修正

app/controllers/articles_controller.rb
class ArticlesController < ApplicationController
  def last
    render json: Article.last, root: false
  end
end

こうなる

{
    "id": 224,
    "title": "Active Model Serializersを試してみる",
    "thumbnail": "http://railscasts.com/static/episodes/stills/409-active-model-serializers.png"
}

カテゴリも欲しいのでhas_oneを追加

app/serializers/article_serializer.rb
class ArticleSerializer < ActiveModel::Serializer
  attributes :id, :title, :thumbnail
  has_one :category
end
{
    "id": 224,
    "title": "Active Model Serializersを試してみる",
    "thumbnail": "http://railscasts.com/static/episodes/stills/409-active-model-serializers.png"
    "category": {
        "id": 4,
        "name": "植物",
        "created_at": "2014-05-12T09:52:20.000Z",
        "updated_at": "2014-05-12T09:52:20.000Z"
    }
}

ちなみにbelongs_toだとundefined methodって言われた。


カテゴリのSerializersを追加

app/serializers/category_serializer.rb
class CategorySerializer < ActiveModel::Serializer
  attributes :id, :name
end
{
    "id": 224,
    "title": "Active Model Serializersを試してみる",
    "thumbnail": "http://railscasts.com/static/episodes/stills/409-active-model-serializers.png"
    "category": {
        "id": 4,
        "name": "植物",
    }
}

カテゴリにメソッド追加

app/serializers/category_serializer.rb
class CategorySerializer < ActiveModel::Serializer
  attributes :id, :name

  def hoge
    "hoge"
  end
end
{
    "id": 224,
    "title": "Active Model Serializersを試してみる",
    "thumbnail": "http://railscasts.com/static/episodes/stills/409-active-model-serializers.png"
    "category": {
        "id": 4,
        "name": "植物",
    }
}

hogeが追加されない。
attributesに:hogeを追加

app/serializers/category_serializer.rb
class CategorySerializer < ActiveModel::Serializer
  attributes :id, :name, :hoge

  def hoge
    "hoge"
  end
end
{
    "id": 224,
    "title": "Active Model Serializersを試してみる",
    "thumbnail": "http://railscasts.com/static/episodes/stills/409-active-model-serializers.png"
    "category": {
        "id": 4,
        "name": "植物",
        "hoge": "hoge",
    }
}

hogeが表示された


参考
https://github.com/rails-api/active_model_serializers
http://railscasts.com/episodes/409-active-model-serializers?language=ja&view=asciicast
http://stackoverflow.com/questions/13125214/active-model-serializers-belongs-to

11
13
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
13