Ash FrameworkAF
Ash Framework3y ago
12 replies
Stefan Wintermeyer

many_to_many update problem

I have a product which has many_to_many tags. Creating works. Updating doesn't. Here's the resource code:

lib/app/shop/resources/product.ex
defmodule App.Shop.Product do
  use Ash.Resource, data_layer: Ash.DataLayer.Ets

  attributes do
    uuid_primary_key :id
    attribute :name, :string
    attribute :price, :decimal
  end

  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, :destroy]

    create :create do
      primary? true
      argument :tags, {:array, :map}
      change manage_relationship(:tags, type: :append_and_remove, on_no_match: :create)
    end

    create :update do
      argument :tags, {:array, :map}
      change manage_relationship(:tags, type: :append_and_remove, on_no_match: :create)
    end
  end

  code_interface do
    define_for App.Shop
    define :create
    define :read
    define :by_name, get_by: [:name], action: :read
    define :update
    define :destroy
  end
end


Here's what I do in the iex:

iex(1)> good_deal_tag = App.Shop.Tag.create!(%{name: "Good deal"})
iex(2)> yellow_tag = App.Shop.Tag.create!(%{name: "Yellow"})
iex(3)> App.Shop.Product.create!(%{
            name: "Banana",
            tags: [good_deal_tag, yellow_tag]
            })
iex(4) banana = App.Shop.Product.by_name!("Banana", load: [:tags])


That all works nicely. But I can not update the products tags or the even the product:

iex(16)> App.Shop.Product.update!(banana, %{tags: [yellow_tag]})
** (FunctionClauseError) no function clause matching in Keyword.split/2
[...]
iex(16)> App.Shop.Product.update!(banana, %{name: "test"})
** (FunctionClauseError) no function clause matching in Keyword.split/2


How can I fix this?
Was this page helpful?