Saving a record with its relationships


#1

Hello, I have this problem: I need to save a relationship along with the record. For eg: If I have a comment with author, frontend sends this data:

{
  "id": null,
  "attributes": { "title": "Rails is Omakase" },
  "relationships": {
    "author": {
      "data": {
        "id": null,
        "attributes": { "name": "New author" },
      }
    }
  }
}

Now, i need to save comment in comments table, and also save author in authors table. does spec allows this? Im unable to find any such example in the specs. I googled, and found some custom solutions, but im not sure if its “logical” to save relationships like this.

Any inputs? Thanks in advance.


#2

I don’t believe this is valid. The way I read the spec a single request can only create a single resource (but that single request can link the new resource to as many existing resources as you like).


#3

Not sure if this is valid but can it be done like this

{
  "data": {
    "type": "comment",
    "attributes": {
      "title": "Rails is Omakase"
    }
  },
  "include": [
    {
      "type": "author",
      "attributes": {
         "name": "New author" 
      }
    }
  ]
}

The only problem now is figuring out how to tell the backend the relationship type. For this case, it’s assuming the relationship is going to be the same as the type “author”