jsonapi-serializer hide the id of a model

488 views Asked by At

Using jsonapi-serializer I don't want to return the ID of the blog post, eventually I'll return a custom slug instead, but I cant figure out how to remove fields in the serializer.

{
    "data": {
        "id": "6",
        "type": "blog_post",
        "attributes": {
            "email": "[email protected]",
            "name": "Mark"
        }
    }
}

I tried using a conditional proc to hide the id to no avail:

class BlogPostSerializer
  include JSONAPI::Serializer
  attributes :email, :name
  attribute :id, if: Proc.new { false }
end
1

There are 1 answers

0
Ashbury On

Ah it turns out you need an ID per the JSON API spec: https://discuss.jsonapi.org

I didn't want to reveal the actual id of the blog post however, so you can tell the serializer to return the uuid instead:

set_id :uuid
attributes :email, :name, :uuid