Skip to content

JR not including type of polymorphic has_one when called to include it #509

Open
@clov3r

Description

@clov3r

The response from the server is missing the embedded target/data/type, or rather replacing it with the empty string...

class Api::V1::ActivityResource < JSONAPI::Resource
  has_one :user
  has_one :target, polymorphic: true
  attributes :previous_value, :new_value, :message, :created_at
  attribute :activity_type   # this is included for now as JSONAPI::Resources sends model type as "activities"
  def activity_type
    @model.type
  end
end
irb(main):001:0> Activity.first
  Activity Load (0.7ms)  SELECT  "activities".* FROM "activities"  ORDER BY "activities"."id" ASC LIMIT 1
=> #<NewRecord id: 1, 
          user_id: nil, 
        target_id: 239, 
      target_type: "Facility",   # <= target_type is correct
   previous_value: nil, 
        new_value: nil, 
          message: nil, 
             type: "NewRecord", 
       created_at: "2015-10-13 15:16:12", 
       updated_at: "2015-10-13 15:16:12">

http://localhost:3000/api/v1/activities/1?include=target,user:

{
    "data": {
        "id": "1",
        "type": "activities",
        "links": {
            "self": "http://localhost:3000/api/v1/activities/1"
        },
        "attributes": {
            "previous-value": null,
            "new-value": null,
            "message": null,
            "created-at": "2015-10-13T15:16:12.964Z",
            "type": "NewRecord"
        },
        "relationships": {
            "user": {
                "links": {
                    "self": "http://localhost:3000/api/v1/activities/1/relationships/user",
                    "related": "http://localhost:3000/api/v1/activities/1/user"
                },
                "data": null
            },
            "target": {
                "links": {
                    "self": "http://localhost:3000/api/v1/activities/1/relationships/target",
                    "related": "http://localhost:3000/api/v1/activities/1/target"
                },
                "data": {
                    "type": "",    // <== should be "Facility"
                    "id": "239"
                }
            }
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions