How to write this dynamically inside an Ecto Query

I have this Ecto Query:

books = (from books in Mango.Books.Book,
         left_join: records in assoc(books, :records),
         where: books.institute_id == ^claims["institute_id"],
         where: not is_nil(records.returned_at), # how to insert "not" or omit it based on the value of params["returned"]
         group_by: [records.book_id, books.id, records.due_for_return, records.returned_at],
         select: %{
           "record_id" => max(records.id),
           "title" => books.title,
           "book_id" => books.id,
           "due_for_return" => records.due_for_return,
           "returned_at" => records.returned_at}
)

|> MangoWeb.Filters.Books.build_query(conn, params)
|> Repo.paginate(page: params["page"], page_size: params["per_page"])

I will be passing to to the controller action a parameter named returned with the value of 0 or 1. On the second where expression I want to either specify not or omit it based on the value of params["retruned"].

Any idea how could this be done? Thank you.

I think the simplest solution would be to compare the is_nil check to a boolean:

returned? = params["returned"] == 1

from books in Mango.Books.Book,
  ...
  where: is_nil(records.returned_at) == ^returned?,
  ...

Depending on actual logic you might want to negate some of the checks, but overall this should work fairly well.

2 Likes