Changing attributes in action

I am trying to simplify the commented out code by using expr() , but i get an InvalidAttributeError with the hint string_downcase(nil)
create :create do
accept [:name]

# what i want, but does not work
change set_attribute(:name, expr(string_downcase(^arg(:name))))

# what works
change fn changeset, _ ->
new_name =
Ash.Changeset.get_argument_or_attribute(changeset, :name)
|> String.downcase()

Ash.Changeset.update_change(changeset, :name, new_name)
end
end
create :create do
accept [:name]

# what i want, but does not work
change set_attribute(:name, expr(string_downcase(^arg(:name))))

# what works
change fn changeset, _ ->
new_name =
Ash.Changeset.get_argument_or_attribute(changeset, :name)
|> String.downcase()

Ash.Changeset.update_change(changeset, :name, new_name)
end
end
Also I am not sure if to put it in before_action or not since from what I saw the change does not appear in the Phoenix form. So in both cases it should only appear in the database ?
2 Replies
Abu kumathra
Abu kumathra12h ago
you don't have any arguments in the action just like the error states maybe replace ^arg(:name) with name
Rise
RiseOP11h ago
iex(6)> Tunez.Music.create_artist(%{name: "Test"})
{:error,
%Ash.Error.Invalid{
bread_crumbs: ["Error returned from: Tunez.Music.Artist.create"],
changeset: "#Changeset<>",
errors: [
%Ash.Error.Changes.InvalidAttribute{
field: :name,
message: "is invalid",
private_vars: nil,
value: string_downcase(name),
has_value?: true,
splode: Ash.Error,
bread_crumbs: ["Error returned from: Tunez.Music.Artist.create"],
vars: [],
path: [],
stacktrace: #Splode.Stacktrace<>,
class: :invalid
}
]
}}
iex(6)> Tunez.Music.create_artist(%{name: "Test"})
{:error,
%Ash.Error.Invalid{
bread_crumbs: ["Error returned from: Tunez.Music.Artist.create"],
changeset: "#Changeset<>",
errors: [
%Ash.Error.Changes.InvalidAttribute{
field: :name,
message: "is invalid",
private_vars: nil,
value: string_downcase(name),
has_value?: true,
splode: Ash.Error,
bread_crumbs: ["Error returned from: Tunez.Music.Artist.create"],
vars: [],
path: [],
stacktrace: #Splode.Stacktrace<>,
class: :invalid
}
]
}}
Unfortunatly not even when it is passed as an argument it fails with value: string_downcase("Test") :/

Did you find this page helpful?