How to add_tag? Managing Relationships

I try to adapt https://ash-hq.org/docs/guides/ash/latest/topics/managing-relationships for an application where a product which has a many_to_many relationship to tags. lib/app/shop/resources/product.ex
defmodule App.Shop.Product do
[...]
relationships do
many_to_many :tags, App.Shop.Tag do
through App.Shop.ProductTag
source_attribute_on_join_resource :product_id
destination_attribute_on_join_resource :tag_id
end
end

actions do
defaults [:read]

create :create do
primary? true
argument :tags, {:array, :map}

argument :add_tag, :map do
allow_nil? false
end

change manage_relationship(:tags, type: :append_and_remove, on_no_match: :create)
change manage_relationship(:add_tag, :tags, type: :create)
end

update :update do
primary? true
argument :tags, {:array, :map}

argument :add_tag, :map do
allow_nil? false
end

change manage_relationship(:tags, type: :append_and_remove, on_no_match: :create)
change manage_relationship(:add_tag, :tags, type: :create)
end
end

code_interface do
define_for App.Shop
define :create
define :read
define :update
end
end
defmodule App.Shop.Product do
[...]
relationships do
many_to_many :tags, App.Shop.Tag do
through App.Shop.ProductTag
source_attribute_on_join_resource :product_id
destination_attribute_on_join_resource :tag_id
end
end

actions do
defaults [:read]

create :create do
primary? true
argument :tags, {:array, :map}

argument :add_tag, :map do
allow_nil? false
end

change manage_relationship(:tags, type: :append_and_remove, on_no_match: :create)
change manage_relationship(:add_tag, :tags, type: :create)
end

update :update do
primary? true
argument :tags, {:array, :map}

argument :add_tag, :map do
allow_nil? false
end

change manage_relationship(:tags, type: :append_and_remove, on_no_match: :create)
change manage_relationship(:add_tag, :tags, type: :create)
end
end

code_interface do
define_for App.Shop
define :create
define :read
define :update
end
end
I get this error:
iex(1)> App.Shop.Product.create!(%{name: "Banana"}, add_tag: %{name: "Yellow"})
** (Ash.Error.Unknown) Unknown Error

* %NimbleOptions.ValidationError{message: "unknown options [:add_tag], valid options are: [:upsert?, :upsert_identity, :upsert_fields, :internal?, :timeout, :tracer, :verbose?, :action, :authorize?, :stacktraces?, :tenant, :actor, :after_action, :return_notifications?, :notification_metadata]", key: [:add_tag], value: nil, keys_path: []}
(ash 2.14.18) lib/ash/api/api.ex:2179: Ash.Api.unwrap_or_raise!/3
iex(1)> App.Shop.Product.create!(%{name: "Banana"}, add_tag: %{name: "Yellow"})
** (Ash.Error.Unknown) Unknown Error

* %NimbleOptions.ValidationError{message: "unknown options [:add_tag], valid options are: [:upsert?, :upsert_identity, :upsert_fields, :internal?, :timeout, :tracer, :verbose?, :action, :authorize?, :stacktraces?, :tenant, :actor, :after_action, :return_notifications?, :notification_metadata]", key: [:add_tag], value: nil, keys_path: []}
(ash 2.14.18) lib/ash/api/api.ex:2179: Ash.Api.unwrap_or_raise!/3
How can I fix that?
Ash HQ
Guide: Managing Relationships
Read the "Managing Relationships" guide on Ash HQ
3 Replies
ZachDaniel
ZachDaniel2y ago
I think you mean for the add_tag to be inside the first argument's map App.Shop.Product.create!(%{name: "Banana", add_tag: %{name: "Yellow"}})
Stefan Wintermeyer
Thank you! I did not see that.
ZachDaniel
ZachDaniel2y ago
also we need to handle that NimbleOptions.ValidationError that is unnecessarily hard to read

Did you find this page helpful?