Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Included relationships of the same class type is broken #32

Open
caseyprovost opened this issue Apr 28, 2018 · 0 comments
Open

Included relationships of the same class type is broken #32

caseyprovost opened this issue Apr 28, 2018 · 0 comments

Comments

@caseyprovost
Copy link

Working through implementing includes I have discovered what appears to be a bug. All my other code like this works as expected and the only difference I can find is that the association is the same class as the requested object.

You will not there is no "included" section even though the relationships section clearly reflects the data.

Controller
users = User.includes(:managers, :direct_reports).page(params[:page]).per(params[:per_page])
render jsonapi: users,
             include: ['managers', 'direct_reports'],
             links: pagination_links(users, User)
Serializer
class SerializableUser < JSONAPI::Serializable::Resource
  type 'users'
  
  belongs_to :role
  belongs_to :parent
  has_many :managers, class 'SerializableUser
  has_many :direct_reports, class 'SerializableUser
end

The response looks nearly correct, except the includes are missing at the end. I have provided a sample below.

{
  "data": [
    {
      "id": "1",
      "type": "users",
      "attributes": {
        "name": "Meghan Wilkinson",
        "email": "[email protected]",
        "threshold": null
      },
      "relationships": {
        "direct_reports": {
          "links": {
            "self": "http://localhost:3000/v1/users/1/relationships/direct_reports",
            "related": "http://localhost:3000/v1/users"
          },
          "data": []
        },
        "managers": {
          "links": {
            "self": "http://localhost:3000/v1/users/1/relationships/managers",
            "related": "http://localhost:3000/v1/users"
          },
          "data": []
        }
      }
    },
    {
      "id": "2",
      "type": "users",
      "attributes": {
        "name": "Dora Nitzsche",
        "email": "[email protected]",
        "threshold": null
      },
      "relationships": {
        "parent": {
          "meta": {
            "included": false
          }
        },
        "role": {
          "meta": {
            "included": false
          }
        },
        "direct_reports": {
          "links": {
            "self": "http://localhost:3000/v1/users/2/relationships/direct_reports",
            "related": "http://localhost:3000/v1/users"
          },
          "data": [
            {
              "type": "users",
              "id": "5"
            }
          ]
        },
        "managers": {
          "links": {
            "self": "http://localhost:3000/v1/users/2/relationships/managers",
            "related": "http://localhost:3000/v1/users"
          },
          "data": [
            {
              "type": "users",
              "id": "4"
            }
          ]
        }
      }
    }
  ],
  "links": {
    "self": "http://localhost/v1/users?page=1&per_page=50",
    "first": "http://localhost/v1/users?page=1&per_page=50",
    "prev": null,
    "next": null,
    "last": "http://localhost/v1/users?page=1&per_page=50"
  },
  "jsonapi": {
    "version": "1.0"
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant