Skip to content

meta repeats when the model contains a meta attribute #1679

Open
@preist

Description

@preist

Description

If a model contains a meta attribute or method, it's repeated twice when the JSON renders (see below). This also happens if a model has a meta:jsonb attribute column.

A simple Todo model serializer

class TodoSerializer < ActiveModel::Serializer
  attributes :id, :title, :description, :meta

  def meta
    {
      created: object.created_at,
      updated: object.updated_at,
      done: object.done
    }
  end
end

The #show controller action:

class TodosController < ApplicationController
  def show
    todo = Todo.find(params[:id])
    render json: todo
  end
end

Response looks like this:

{
    "todo": {
        "id": 1,
        "title": "Example Todo",
        "description": "Example description",
        "meta": {
            "created": "2016-04-12T08:24:58.336Z",
            "updated": "2016-04-12T08:24:58.336Z",
            "done": false
        }
    },
    "meta": {
        "created": "2016-04-12T08:24:58.336Z",
        "updated": "2016-04-12T08:24:58.336Z",
        "done": false
    }
}

It seems that the model meta overwrites the ActiveModelSerializer one.

Environment

Clean Rails 4.2.6 installation and active_model_serializers 0.9.4

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions