I know how I can use dynamic fields and values into Ecto query, for example:
field = :age
value = 20
Ecto.Query.where(App.User, [x], field(x, ^field) < ^value)
But, it is possible to define the operator (in this example <
) dynamically? and how?
I have tried it with fragment
and interpolated string:
operator = ">"
Ecto.Query.where(App.User, [x], fragment("? #{operator} ?", field(x, ^field), ^value))
but it raises the exception: Ecto.Query.CompileError) to prevent SQL injection attacks, fragment(...) does not allow strings...
Ecto.Query.where(User, [x], custom_where(x, field, value, sign)
. It shows this error:(Ecto.Query.CompileError) sign(field(x, ^field), ^value) is not a valid query expression
Any suggest ? – Shahjahanpur