gordoneliel
I have a resource, eg Post, where the post has many Authors. When creating a Post, there should be at least one (1) Author attached to the payload. I know the json api v1 spec does not really outline creating multiple resources at once, but I figured we should be able to regardless?
What I’ve tried so far:
- Passing the relationship in the relationships section of the body - This fails with stack
[error] GenServer #PID<0.767.0> terminating
** (FunctionClauseError) no function clause matching in anonymous fn/2 in AshJsonApi.Request.relationship_change_value/1
(ash_json_api 0.33.1) lib/ash_json_api/request.ex:668: anonymous fn(:error, {:ok, []}) in AshJsonApi.Request.relationship_change_value/1
(elixir 1.15.7) lib/enum.ex:4387: anonymous fn/3 in Enum.reduce/3
- Passing the relationship into the
Postattributes when creating - This does not work, throws an invalid body because theAuthorattribute is not an attribute on the resource but rather a relationship.
I combed through the relationships section of Ash and AshJsonApi but cant seem to find anything related to creating multiple related resources.
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself.
My main conc...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 13 to 4- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
zachdaniel
You can simplify a bit if you do
then they can provide the
post_iddirectly as an input. ThenAnd then use that one action in both. Since
post_idis notnullable, you will always be prevented from creating a comment with no post, and themanage_relationshipwill setpost_idautomatically.gordoneliel
Thats not bad. Although, would I have to have 2 create actions? When creating a
Post, I create aCommentalongside through the managed relationship, but If I also want to create aCommentfor aPost, I need another create action that specifies an argumentpost, :map, allow_nil? false.Eg.
zachdaniel
Right, so this is for editing the relationship, its
/post/:id/relationships/comments. However, that isn’t for creating new things, I just reread your initial post and now I see what you mean. This should likely be done asPOST /commentsspecifying a post id to be the most REST-ish. We can add the option to ignore the base-route for some routes, that way on comment you could do:create :create, route: "/post/:post_id/comments", base_route?: falseand have it pass thepost_idthrough.gordoneliel
I do have that on the Post resource, but getting a
no route foundwhen hitting POST/post/:id/commentszachdaniel
So, that part is actually still based on old behavior, and probably needs to be upgraded at some point. What it does is use the primary action and explicitly adds
Changeset.manage_relationship(instead of it being an argument in an action).So you can do this in routes:
gordoneliel
Honestly not sure what the idiomatic way for JSONApi would be.
Should I have a
PATCHonPosteg/post/:id/comment, route: "/:id/contributors"(this is on post json_api routes), and an action on thePostresource that delegates creation of theCommentthrough the managed relationship? Or should it be aPOSTon/post/:id/commentsthat goes directly to theCommentcreate action?Example for reference:
gordoneliel
On a similar note, I have a situation where I have to create a
Comment.When creating a comment it should have the associated
Postit 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 aroute not founderror.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 theCommentresource, one for when creating the comment through aPost(I would like to be able to create a comment when creating a new Post), and another when creating a comment after aPosthas been created.gordoneliel
I think that would work! The json api spec for v1 is a little weird with this, I wonder if the upcoming v1.2 spec makes it easier to do this
zachdaniel
Okay, I can see it now, yeah. It should be in the relationships, but the issue is that we are currently requiring
id, which doesn’t make sense in all contexts. I’ve just pushed something tomainto make those optional. Additionally, extra attributes have to go in the"meta"key of the relationship, i.e{type: "type", "meta" => {foo: "bar"}}. This is required by the spec, IIRC.gordoneliel
Getting:
Expected only defined properties, got key ["data", "attributes", "authors"].This is my create action: