Active Model Serializersを試してみる

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

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

```rb:app/models/article.rb
class Article < ActiveRecord::Base
  belongs_to :category
end
```

```json
{
    "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

```text:Gemfile
gem "active_model_serializers"
```

article_serializer.rbの追加

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

これだけ。jsonの確認

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

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

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

こうなる

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

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

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

```json
{
    "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を追加

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

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

***
カテゴリにメソッド追加

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

  def hoge
    "hoge"
  end
end
```

```json
{
    "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を追加

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

  def hoge
    "hoge"
  end
end
```

```json
{
    "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