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
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
Popular in Questions
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
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
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
In templates/appointment/index.html.eex:
<%= for appointment <- @appointments do %>
<tr>
<td><%= appoi...
New
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
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
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
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
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
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
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
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
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
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex









