acrolink

acrolink

How to handle a nil parameter inside an Ecto database query

I have created some basic query to load data from the database:

page =
Policy
|> join(:left, [p], c in Client, p.client_id == c.id)
|> where([p, c], c.user_id == ^conn.assigns.current_user.id)
|> where([p, c], p.client_id in ^params["policy"]["client_id"])
|> preload([p, c], [client: c])
|> Repo.paginate(params)

The nil problem arises when the GET parameter ["policy"]["client_id"] is missing (not set in the request). My question, how to handle that? How to prevent this error? Basically I want that particular where sentence to be ignored if ["policy"]["client_id"] is empty.

Marked As Solved

jwarlander

jwarlander

You could also use query composition, pushing out the client_id filtering to a separate function.. should look something like this:

def filter_policy_client_id(query, nil), do: query
def filter_policy_client_id(query, client_id) do
  query |> where([p], p.client_id in ^client_id)
end

# ...

page =
Policy
|> join(:left, [p], c in Client, p.client_id == c.id)
|> where([p, c], c.user_id == ^conn.assigns.current_user.id)
|> filter_policy_client_id(params["policy"]["client_id"])
|> preload([p, c], [client: c])
|> Repo.paginate(query, params)

Is client_id supposed to be a list? If it’s not, I suspect you should use equality comparison instead of in.

Also Liked

schaary

schaary

I would encapsulate this query in a separate function and pattern match on the parameter

def foo(params["policy"]["client_id"] = client_id) when is_binary(client_id) do
  # query the db
end

def foo(_) do
  # return some thing useful
end
  

Last Post!

acrolink

acrolink

@schaary, @jwarlander
Thank you.. I have learned new things in Elixir today inspecting the answers you have provided..

Where Next?

Popular in Questions Top

vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
lanycrost
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
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
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
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
dblack
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
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

We're in Beta

About us Mission Statement