# AshJsonApi: Create relationship when creating resource

**URL:** <https://forum.elixirforum.com/t/ashjsonapi-create-relationship-when-creating-resource/60040>\
**Category:** Questions\
**Tags:** ash\
**Created:** [November 29, 2023, 8:51pm UTC](https://forum.elixirforum.com/t/ashjsonapi-create-relationship-when-creating-resource/60040 "2023-11-29T20:51:24Z")\
**Posts on this page:** 1\
**Showing post:** 8

<div class="post-metadata">

**Author:** ![gordoneliel](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/gordoneliel/32/30383_2.png) [@gordoneliel](https://forum.elixirforum.com/u/gordoneliel)\
**Post date:** [December 5, 2023, 4:57pm UTC](https://forum.elixirforum.com/t/ashjsonapi-create-relationship-when-creating-resource/60040/8 "2023-12-05T16:57:51Z")

</div>

On a similar note, I have a situation where I have to create a `Comment`.

When creating a comment it should have the associated `Post` it belongs to. I know we can get the data on a related resource for eg. GET `/post/:id/comments`, but when I try to send a ‘POST’ request to the same url, I get a `route not found` error.

Question is, how do we post to a related resource? I tried just posting to the direct resource url, eg `/comments`, but then I have to manually attach the relationship, and create two different actions on the `Comment` resource, one for when creating the comment through a `Post` (I would like to be able to create a comment when creating a new Post), and another when creating a comment after a `Post` has been created.

---

_[View the full topic](https://forum.elixirforum.com/t/ashjsonapi-create-relationship-when-creating-resource/60040)._
