acrolink

acrolink

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.

Most Liked

michalmuskala

michalmuskala

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.

Where Next?

Popular in Questions Top

minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New

We're in Beta

About us Mission Statement