Skip to content

Latest commit

 

History

History
49 lines (37 loc) · 1.23 KB

File metadata and controls

49 lines (37 loc) · 1.23 KB

Active Model Serializers

JSON API Adapter

When using the JSON API adapter, you won't get any nested resources. This is a feature, not a bug. You don't nest resources in the JSON API spec.

Serializers

class AuthorSerializer < ActiveModel::Serializer
  has_many ::books
end

class BookSerializer < ActiveModel::Serializer
  attribute(:publisher_name) { object.publisher.name }
end

API Response

"relationships": {
  "books": {
    "data": [
      {
        "id": "821",
        "type": "books"
      },

The attributes won't get returned! #JSONAPISpec

Nested Resources

There's a bug when showing nested resources with the JSON API adapter in Active Model Serializers. It does a deeply nested n+1 query by default.

To get away from the n+1, you can specify an include_data false inside of the relationship definition.

Avoiding the n+1 query bug

class PostSerializer < ActiveModel::Serializer
  attributes :title, :body
  has_many :comments do
    link(:related)  { post_comments_path(post_id: object.id) }
    include_data false
  end
end