gordoneliel

gordoneliel

AshJsonApi: Create relationship when creating resource

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:

  1. 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
  1. Passing the relationship into the Post attributes when creating - This does not work, throws an invalid body because the Author attribute 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.

Marked As Solved

zachdaniel

zachdaniel

Creator of Ash

You can simplify a bit if you do

belongs_to :post, Post do
  attribute_writable? true
end

then they can provide the post_id directly as an input. Then

create :create do
  accept [..., :post_id]
  primary? true
end

And then use that one action in both. Since post_id is not nullable, you will always be prevented from creating a comment with no post, and the manage_relationship will set post_id automatically.

Also Liked

zachdaniel

zachdaniel

Creator of Ash

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 to main to 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

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

Where Next?

Popular in Questions Top

Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
Kurisu
For example for a current url like http://localhost:4000/cosmetic/products?_utf8=✓&amp;query=perfume&amp;page=2, I would like to get: ...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
beno
I will often find my self writing things similar to: case some_value do nil -&gt; something() "" -&gt; something() _ -&gt; somethi...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

Other popular topics Top

lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
Qqwy
Update: How to use the Blogs &amp; Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 126479 1222
New

We're in Beta

About us Mission Statement