I try to search in a column based on the user’s language.
I have 3 entities to search in for the relation which is based on the language, but unfortunately I could not get it working.
My first try was
dynamic([m, s], ilike(m.n, ^"%#{term}%") or ilike(m.l, ^"%#{term}%") or ilike(^type_name_column(locale), ^"%#{term}%"))
defp type_name_column(locale) do
case locale do
"de" -> "m.de"
_ -> "m.en"
end
end
It worked for one entity, but don’t not work for the other one. Whyever.
My second try was to move the call outside dynamic
:
dynamic([m, s], ilike(m.n, ^"%#{term}%") or ilike(m.l, ^"%#{term}%") or ^type_name_ilike(locale, term))
defp type_name_ilike(locale, param) do
case locale do
"de" -> ilike("m.de", ^"%#{param}%"))
_ -> ilike("m.en", ^"%#{param}%"))
end
end
But this did not even compile anymore.
So I finally came up with my the third approach that would work again, but I am not happy with the code since I need to write this helper function a few times since I have different counts of the dynamic parameters.
dynamic([m, s], ilike(m.n, ^"%#{term}%") or ilike(m.l, ^"%#{term}%") or ^type_name_ilike(locale, term))
defp type_name_ilike(locale, param) do
case locale do
"de" -> dynamic([m, s], ilike(s.de, ^"%#{param}%"))
_ -> dynamic([m, s], ilike(s.en, ^"%#{param}%"))
end
end
What’s the best way to solve this problem?