For instance, certain attributes are stored in the database as utc_datetime, and I need to assign their values conditionally based on a boolean argument.
defmodule MyApp.AshDomains.Accounts.Preferences do
use Ash.Resource,
domain: MyApp.AshDomains.Accounts,
data_layer: AshPostgres.DataLayer,
extensions: [AshArchival.Resource]
alias Ash.Changeset
postgres do
table "account_preferences"
repo MyApp.Repo
end
archive do
attribute :deleted_at
end
changes do
change fn changeset, _context ->
notification_enabled_at = Changeset.get_attribute(changeset, :notification_enabled_at)
notification_enabled = Changeset.get_argument(changeset, :notification_enabled)
cond do
is_nil(notification_enabled_at) and notification_enabled == true ->
Changeset.force_change_attribute(changeset, :notification_enabled_at, expr(now()))
not is_nil(notification_enabled_at) and notification_enabled == false ->
Changeset.force_change_attribute(changeset, :notification_enabled_at, nil)
true ->
changeset
end
end
end
actions do
defaults [:read, :destroy]
create :create do
argument :notification_enabled, :boolean, allow_nil?: false
end
update :update do
argument :notification_enabled, :boolean, allow_nil?: false
end
end
attributes do
uuid_v7_primary_key :id
attribute :notification_enabled_at, :utc_datetime
end
calculations do
calculate :notification_enabled, :boolean, expr(not is_nil(notification_enabled_at))
end
end
When I try to set the attribute value, I get an error about implementing atomic/3, but I don’t think it should be atomic, it’s just a derived value.
[warning] Unhandled error in form submission for Salao365.AshDomains.Appointments.Product.update
This error was unhandled because Ash.Error.Framework.MustBeAtomic does not implement the
AshPhoenix.FormData.Errorprotocol.** (Ash.Error.Framework.MustBeAtomic) Salao365.AshDomains.Appointments.Product.update must be performed atomically, but it could not be
Reason: Ash.Resource.Change.Function does not implement
atomic/3See Update Actions — ash v3.6.2 for more on atomics.
@zachdaniel do you have any suggestions?
I was expecting something like this:
change set_new_attribute(:notification_enabled_at, expr(if arg(:notification_enabled) == true, do: now(), else: nil))




















