Active_model_serializers: Integer attribute was converted to string

Created on 16 Sep 2016  路  2Comments  路  Source: rails-api/active_model_serializers

Expected behavior vs actual behavior

I'm new to AMS. I created a table named 'tags'. Here it is.

create_table "tags", force: :cascade do |t|
    t.string   "name"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
end

The data type of id is Integer. However the output is String.

Expected:
"data": [
  {
    "id": 1,
    "type": "tags",
    "attributes": {
    "name": "autem"
     }
  }
]
Actual:
"data": [
  {
    "id": "1",
    "type": "tags",
    "attributes": {
    "name": "autem"
    }
  }
]

Steps to reproduce

  • Model
class Tag < ApplicationRecord
end
  • Controller
module Api
  module V1
    class TagsController < ApplicationController
      def index
        @tags = Tag.all
        render json: @tags
      end
    end
  end
end
  • Serializer
class TagSerializer < ActiveModel::Serializer
  attributes :id, :name
end

Environment

ActiveModelSerializers Version _0.10.0_:

Output of ruby -e "puts RUBY_DESCRIPTION": ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-darwin15]

OS Type & Version: EI Capitan 10.11.2

Integrated application and version _(e.g., Rails, Grape, etc)_:

  • Rails: 5.0.0.1

Backtrace

_(e.g., provide any applicable backtraces from your application)_

None.

Thank U in advance.

Most helpful comment

Hi @polarlights - looks like you're outputting jsonapi. According to the jsonapi spec, id must be a string: http://jsonapi.org/format/#document-resource-objects

All 2 comments

Hi @polarlights - looks like you're outputting jsonapi. According to the jsonapi spec, id must be a string: http://jsonapi.org/format/#document-resource-objects

Yep. My fault. @richmolj Thank you very much! 馃憤

Was this page helpful?
0 / 5 - 0 ratings