cerebris / jsonapi-resources

A resource-focused Rails library for developing JSON:API compliant servers.
http://jsonapi-resources.com
MIT License
2.32k stars 533 forks source link

Is there a way to include records that are not associated to primary model? #275

Closed danielspaniel closed 9 years ago

danielspaniel commented 9 years ago

I am trying to 'include' ( side load ) records that are not directly ( though indirectly ) related to the primary resource object being serialized. Is there a way to do that? I tried to using the includes hash, but if that have does not find the association in the resource, it ignores it.

  class ProjectResource < JSONAPI::Resource
     has_many :sites
  end

  include_resources = ['sites', 'buildings']
  json = JSONAPI::ResourceSerializer.new(
      ProjectResource, include: include_resources
  ).serialize_to_hash(ProjectResource.new(project))

I want to include the buildings but they are not part of the project model directly, so they don't show up. I could do 'sites.buildings' but I want to avoid too much nesting, since the buildings have the has_one :site already.

Does that make sense?

I am arriving at jsonapi from the active model serializer world where you could side load anything you wanted with a payload.

danielspaniel commented 9 years ago

I am seeing that the answer is no .. since the jsonapi structure is not so loosey goosey

toobulkeh commented 8 years ago

I don't think this has anything to do with "loosey goosey" -- The following is perfectly valid JSONapi:

{
    "data": {
        "id": "1",
        "type": "tickets",
        "links": {
            "self": "http://localhost:3000/v1/tickets/1"
        },
        "attributes": {
            "title": "Lomo 8-bit vinegar carry listicle pork belly vinyl.",
            "description": "I'll copy the solid state COM pixel, that should card the THX alarm!",
            "queue": "billing-questions",
            "severity": "critical",
            "status": "open",
            "autotask-id": "T20160113.0001",
            "created-at": "2016-01-28T16:56:38Z",
            "updated-at": "2016-01-28T16:56:38Z"
        },
        "relationships": {
            "creator": {
                "links": {
                    "self": "http://localhost:3000/v1/tickets/1/relationships/creator",
                    "related": "http://localhost:3000/v1/tickets/1/creator"
                },
                "data": {
                    "type": "users",
                    "id": "1"
                }
            },
            "notes": {
                "links": {
                    "self": "http://localhost:3000/v1/tickets/1/relationships/notes",
                    "related": "http://localhost:3000/v1/tickets/1/notes"
                }
            }
        }
    },
    "included": [
        {
            "id": "1",
            "type": "users",
            "links": {
                "self": "http://localhost:3000/v1/users/1"
            },
            "attributes": {
                "first-name": "Bertram",
                "last-name": "Haley",
                "email": "mylene@cremin.org",
                "phone": "(746) 565-9521 x17021"
            },
            "relationships": {
                "account": {
                    "links": {
                        "self": "http://localhost:3000/v1/users/1/relationships/account",
                        "related": "http://localhost:3000/v1/users/1/account"
                    },
                    "data": {
                        "type": "accounts",
                        "id": "1"
                    }
                }
            }
        },
        {
            "id": "1",
            "type": "accounts",
            "links": {
                "self": "http://localhost:3000/v1/accounts/1"
            },
            "attributes": {
                "name": "Account 1",
            }
        }
    ]
}
danielspaniel commented 8 years ago

I think your saying you can sideload what ever you want. For some reason it did not work for me, but I will revisit when I get back to trying JSON API serializer again.

toobulkeh commented 8 years ago

Oh -- I'm just saying the JSONapi SPEC allows for this to happen, but the jsonapi-resources rails gem does not have that functionality... yet

toobulkeh commented 8 years ago

:+1: for this as a feature request, by the way. It would be very valuable to us. As far as the way to do it.. that's a different discussion.

danielspaniel commented 8 years ago

Hmm .. yeah .. good points.