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 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
Hi,
I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors:
[WARN] - (starship::utils): Executing command ...
New
Hey,
Just curious what are the main benefits of Elixir compared to Clojure?
When is Elixir more useful than Clojure and vice versa?
Th...
New
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
Hi!
In PHP: $SERVER['SERVERADDR'] - in Elixir?
Searched the docs for ip address and the web, no good results.
Thanks!
New
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar.
I p...
New
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
Hi! May someone helps me, please!
I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
Other popular topics
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
New
Hello, how can I check the Phoenix version ?
Thanks !
New
Hi folks,
Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
New
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar.
I p...
New
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
Hi everyone!
I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New








