Я пишу запрос для фильтрации платежей.Мы можем выполнить фильтрацию по имени платежа, состоянию, дате от, дате, сумме от и сумме до.Я получил запросы, работающие для всех, кроме суммы от и до суммы, поскольку они немного сложнее и включают использование соединения / подзапроса.
A payment
относится к payment_method
, который содержитинформация для суммы, которая была отправлена.Это отношение один-к-одному, когда payment
ссылается на идентификатор payment_method
с идентификатором funding_id.
Мне нужно иметь возможность отфильтровать все payments
, попадающие в диапазон значений.
Вот часть запроса, в которой мне нужна помощь:
def search(user_id, params) do
#MAIN QUERY
q = Payments.Schema
|> where([user_id: ^user_id])
|> where(^filter_name(params[:name]))
|> where(^filter_status(params[:status]))
|> where(^filter_date_from(params[:date_from]))
|> where(^filter_date_to(params[:date_to]))
|> join(:inner, ^filter_amount_from(params[:amount_from], params[:amount_to]))
#THIS FUNCTION DOESN'T CURRENTLY WORK
defp filter_amount(amount_from, amount_to) when is_integer(amount_from) and is_integer(amount_to) do
dynamic([t], t.funding_id in subquery(
p = PaymentMethods.Schema
|> where(p.amount >= ^amount_from)
|> where(p.amount <= ^amount_to)
), t.funding_id = p.id)
end
defp filter_amount(_amount_from, _amount_to), do: true
В настоящее время я получаю эту ошибку в терминале:
== Compilation error in file lib/users/payments/payments.ex ==
** (Ecto.Query.CompileError) unbound variable `p` in query
Любая помощь будет принята с благодарностью!
ОБНОВЛЕНИЕ
Я изменил код на следующий:
def search(user_id, params) do
#MAIN QUERY
q = Payments.Schema
|> where([user_id: ^user_id])
|> where(^filter_name(params[:name]))
|> where(^filter_status(params[:status]))
|> where(^filter_date_from(params[:date_from]))
|> where(^filter_date_to(params[:date_to]))
|> join(:inner, [t], p in subquery(^filter_amount(params[:amount_from], params[:amount_to]), t.funding_id == p.id))
defp filter_amount(amount_from, amount_to) when is_integer(amount_from) and is_integer(amount_to) do
PullTransactions.Schema
|> where(pt.amount >= ^amount_from)
|> where(pt.amount <= ^amount_to)
end
defp filter_amount(_amount_from, _amount_to), do: true
Ошибка, которую я получаю в терминале, заключается в следующем:
cannot use ^filter_amount(params[:amount_from], params[:amount_to]) outside of match clauses