AF
Ash Framework•3mo ago
Ege

Ash.Error.Unknown.UnknownError: Invalid reference

I have this action in the UserUpload resource. I added another criteria for filtering the resource by the first or last name of its user relationship. However, the String.contains? lines give this error:
11:29:38.679 request_id=GFIrFgeBN9WstPIAASCB remote_ip=127.0.0.1 [error] ** (MatchError) no match of right hand side value: {:error, %Ash.Error.Unknown{errors: [%Ash.Error.Unknown.UnknownError{error: "Invalid reference user.first_name at relationship_path [:user]", field: nil, value: nil, splode: Ash.Error, bread_crumbs: [], vars: [], path: [:filter], stacktrace: #Splode.Stacktrace<>, class: :unknown}]}}
11:29:38.679 request_id=GFIrFgeBN9WstPIAASCB remote_ip=127.0.0.1 [error] ** (MatchError) no match of right hand side value: {:error, %Ash.Error.Unknown{errors: [%Ash.Error.Unknown.UnknownError{error: "Invalid reference user.first_name at relationship_path [:user]", field: nil, value: nil, splode: Ash.Error, bread_crumbs: [], vars: [], path: [:filter], stacktrace: #Splode.Stacktrace<>, class: :unknown}]}}
The relationship and its first_name and last_name fields are public?: true
No description
7 Replies
ZachDaniel
ZachDaniel•3mo ago
String.contains? is not valid in expressions
Ege
EgeOP•3mo ago
What should I be using instead
ZachDaniel
ZachDaniel•3mo ago
I'm checking the expressions guide 😄 It is just contains/2 https://hexdocs.pm/ash/expressions.html#functions
Ege
EgeOP•3mo ago
Thanks! What's the best way to deal with the search argument being nil or ""? I want to ignore it in those situations
ZachDaniel
ZachDaniel•3mo ago
The way you're doing it now is reasonable, but often times "regular-old-elixir" is cleaner
prepare fn query, _ ->
if query.arguments[:search] do
Ash.Query.filter(...)
else
query
end
end
prepare fn query, _ ->
if query.arguments[:search] do
Ash.Query.filter(...)
else
query
end
end
Ege
EgeOP•3mo ago
I get function Ash.Query.filter/1 is undefined or private even though I have require Ash.Query in the module Ah, it's filter/2 How do I make it case-insensitive @Zach Daniel ? It doesn't like it when I try String.downcase(user.first_name) inside the expr
prepare fn query, _ ->
if query.arguments[:search] do
Ash.Query.filter(query,
expr(
contains(user.first_name, ^query.arguments[:search]) or
contains(user.last_name, ^query.arguments[:search])
)
)
else
query
end
end
prepare fn query, _ ->
if query.arguments[:search] do
Ash.Query.filter(query,
expr(
contains(user.first_name, ^query.arguments[:search]) or
contains(user.last_name, ^query.arguments[:search])
)
)
else
query
end
end
Based on https://hexdocs.pm/ash/Ash.Query.Function.Contains.html I also tried:
prepare fn query, _ ->
if query.arguments[:search] do
Ash.Query.filter(query,
expr(
contains(%Ash.CiString{:string, user.first_name}, ^query.arguments[:search]) or
contains(%Ash.CiString{:string, user.last_name}, ^query.arguments[:search])
)
)
else
query
end
end
prepare fn query, _ ->
if query.arguments[:search] do
Ash.Query.filter(query,
expr(
contains(%Ash.CiString{:string, user.first_name}, ^query.arguments[:search]) or
contains(%Ash.CiString{:string, user.last_name}, ^query.arguments[:search])
)
)
else
query
end
end
But this one doesn't compile Sigh. string_downcase/1
ZachDaniel
ZachDaniel•3mo ago
Something like this would also likely work:
Ash.Query.filter(query,
expr(
contains(type(user.first_name, :cistring), ^query.arguments[:search]) or
contains(type(user.last_name, :cistring), ^query.arguments[:search])
)
)
Ash.Query.filter(query,
expr(
contains(type(user.first_name, :cistring), ^query.arguments[:search]) or
contains(type(user.last_name, :cistring), ^query.arguments[:search])
)
)

Did you find this page helpful?