AE
Ash Elixir•3y ago
Blibs

Are accept fields in an update action required by default?

I have the following update action:
update :update_images do
accept [:id, :status, :images]

change set_attribute(:status, arg(:status), set_when_nil?: false)
change set_attribute(:images, arg(:images))
end
update :update_images do
accept [:id, :status, :images]

change set_attribute(:status, arg(:status), set_when_nil?: false)
change set_attribute(:images, arg(:images))
end
I would expect that in this case the status is a optional field and will be nil if I don't send it as an action argument. But if I do that, I actually get the following error:
%Ash.Error.Changes.Required{
field: :status,
type: :attribute,
resource: Marketplace.Markets.Property,
changeset: nil,
query: nil,
error_context: [],
vars: [],
path: [],
stacktrace: #Stacktrace<>,
class: :invalid
}
%Ash.Error.Changes.Required{
field: :status,
type: :attribute,
resource: Marketplace.Markets.Property,
changeset: nil,
query: nil,
error_context: [],
vars: [],
path: [],
stacktrace: #Stacktrace<>,
class: :invalid
}
An I missing something of this is the expected behavior?
4 Replies
ZachDaniel
ZachDaniel•3y ago
Shouldn't be. Perhaps set_when_nil? is not doing what you expect? Separately, arg(;status) has no effect if there is no status argument if you do accept you don't need to add arguments but in your case it seems like you want to not set it if its nil so I'd do:
accept [:id, :images]
argument :status, :type

change set_attribute(:status, arg(:status), set_when_nil?: false)
change set_attribute(:images, arg(:images))
accept [:id, :images]
argument :status, :type

change set_attribute(:status, arg(:status), set_when_nil?: false)
change set_attribute(:images, arg(:images))
Seems like a strange pattern. Wouldn't they just leave out the status if they don't want to change it?
Blibs
BlibsOP•3y ago
You mean that the update can be like this? update :update_images do accept [:id, :status, :images] end Since status and images are fields inside the resource?
ZachDaniel
ZachDaniel•3y ago
Yes, accept means "these attributes are writable" By default the accept list is all public writable attributes, but that will change in the next major version
Blibs
BlibsOP•3y ago
Makes sense, and it worked 😄

Did you find this page helpful?