Hello,
I am learning ash with phoenix live view. I am unable to create to create action for a table having multiple relationships. Please go through below.
I have a demand resource where table has slots_id
, user_id
and booking_volume
.
attributes do
uuid_primary_key :id
attribute :booked_volume, :integer do
allow_nil? false
end
end
relationships do
belongs_to :user,User do
allow_nil? false
end
belongs_to :slots,Slots do
allow_nil? false
end
end
For create action( default i am only accepting :booked_volume
for the input field.). For user ID, i am providing @current_user
as a actor. So for slot_id. I tried 2 things:
- Add one more relate_actor in the change action like, i also made sure i pass :
actions do
default_accept [:booked_volume]
changes do
change relate_actor(:user)
change relate_actor(:slots)
end
and i passed as 2 actors in the create for like, i also made sure that slot contains slot model in the live view:
defp assign_form(%{assigns: %{slot: slot, current_user: current_user}} = socket) do
form =
AshPhoenix.Form.for_create(
Lngx.DemandAggregation.UserDemand.Demand,
:create,
as: "create_demand",
actor: current_user, slot
)
assign(socket, form: to_form(form))
end
The error i am getting is: [error] Unable to Create Demand: [slots: {"is required", []}]
2. Later i thought actor can be only one. So i tried to manually put slot in the map while inserting by passing slot_id
from assigns. Still i am getting the similar error like above: [error] Unable to Create Demand: [slots: {"is required", []}]
Here the function to for create in :live_view
:
def handle_event("save", %{"create_demand" => create_demand}, socket) do
# Logger.warning("Socket Assigns: #{inspect(socket.assigns)}")
create_demand_with_slot_id = Map.put(create_demand, :slots_id, socket.assigns.id)
case AshPhoenix.Form.submit(socket.assigns.form, params: create_demand_with_slot_id) do
{:ok, _demand} ->
socket
|> put_flash(:info, "Created Demand Succesfully")
|> push_patch(to: socket.assigns.patch)
{:noreply, socket}
{:error, form} ->
Logger.error("Unable to Create Demand: #{inspect(form.errors)}")
{:noreply, assign(socket, form: form)}
end
end
How can I solve this? Any help or references would be greatly appreciated.
Thank You.