Maybe something like Ecto.Multi might help. It would look like this
alias Ecto.Multi
@spec create_registration(%{binary => term} | %{atom => term}) :: {:ok, %{registration: %Registration{}, check_primary: :ok}} | {:error, :registration | :check_primary, Ecto.Changeset.t | :error, changes_so_far :: %{optional(atom) => term}}
def create_registration(attrs) do
Multi.new()
|> Multi.insert(:registration, Registration.changeset(%Registration{}, attrs))
|> Multi.run(:check_primary, fn %{registration: registration} -> check_primary(registration) end)
|> Repo.transaction()
end
which would rollback if check_primary(registration) returns :error.
Checking them separately does not help.
Why? I thought it’d work … Can you show the code that you’ve used?
I need to compute fees for the participant but that can happen only after arrival_date and departure_date changes, if any, are applied to the record
That also could be done either in a multi or through a changeset, I prefer changesets:
@spec put_fees(Ecto.Changeset.t) :: Ecto.Changeset.t
defp put_fees(%{valid?: true, changes:%{arrival_date: arrival_date, departure_date: departure_date}} = changeset) do
put_change(changeset, :fees, compute_fees(arrival_date, departure_date))
end
defp put_fees(changeset), do: changeset






















