Skip to content

sideposting - associating same newly created record with multiple records that are also being created #435

Description

@akshah123

I have a usecase with sidepost where I would multiple records to reference a new record that's being created.

An example setup would be:

class ModelA
  has_many :model_b
end

class ModelB
  belongs_to :model_a
  has_many :model_c
end

class ModelC
  belongs_to :model_b
  belongs_to :model_a
end

Now, i would like to create three object(s). 1 ModelA, 1 ModelB that belongs to ModelA and 1 ModelC that belongs to A & B.

I am trying following POST:

{
  "data": {
    "temp-id": "aaaa",
    "type": "model_a",
    "attributes": {
      "street_and_number": "test_model_a"
    },
    "relationships": {
      "model_b": {
        "data": [
          {
            "type": "model_b",
            "temp-id": "1a1a1a",
            "method": "create"
          }
        ]
      }
    }
  },
  "included": [
    {
      "type": "model_b",
      "temp-id": "1a1a1a",
      "attributes": {
        "name": "test location"
      },
      "relationships": {
        "model_c": {
          "data": [
            {
              "type": "model_c",
              "temp-id": "2b2b2b",
              "method": "create"
            }
          ]
        }
      }
    },
    {
      "type": "model_c",
      "temp-id": "2b2b2b",
      "attributes": {},
      "relationships": {
        "model_a": {
          "data": {
            "type": "model_a",
            "temp-id": "aaaa",
            "method": "create"
          }
        }
      }
    }
  ]
}

However, above setup seems to create duplicate records for ModelA. How would I set this up so that I am not creating duplicate records while keeping it a single API call?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugVerified to reproduce; see needs-repro for unconfirmedneeds reproNeeds a reproduction before triage

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions