Ash FrameworkAF
Ash Framework3mo ago
10 replies
matt_savvy

Adding an association in an action based on argument

Hey, been a few minutes since I've done any work on an Ash project, hoping someone can jog my memory on how to accomplish something.

Basically, I've got two resources, Maps.Map and Pin , where a Pin belongs to a Map. I want to pass an existing Map to the Pin as part of the create action. like so
# usage (via code interface)
Maps.Map.add_pin_to_map(some_map, attrs)


Been searching the docs, but I can't really figure out how to take my argument and add the association
# maps/pin.ex
actions do
  create :add_to_map do
      argument :map, :struct,
        allow_nil?: false,
        constraints: [instance_of: Maps.Map]
  end
end

relationships do
  belongs_to :map, Maps.Map
end


I was thinking it's something like using change like so, but not quite
# raises BadMapError
change set_attribute(:map_id, arg(:map).id)
# also raises BadMapError
      change fn changeset, context ->
        map = arg(:map)
        Ash.Changeset.change_attribute(:map_id, map.id)
      end


Thanks!
Was this page helpful?