Search code examples
elixirphoenix-frameworkecto

Conditional parameters in an Ecto Query


I've got a parameter platform which is optional:

def query_clicks(freq \\ "day", platform \\ false) do
    from(Click)
    |> select(
      [c],
      [
        fragment("date_trunc(?,?) as t", ^freq, c.inserted_at), count(c.link_id) ]
      )
      |> if platform, do: fn(q) -> where([c], c.platform == ^platform) end, else: fn(q): q end
    |> group_by([c], fragment("t"))
    |> Repo.all
  end

I've tried to hack something in (see "if platform..."), but I'm not sure of the exact syntax to use. What I want to do is:

if platform != None:
  return query + WHERE statement
else:
  return query

What's the right syntax?


Solution

  • You could add an extra function maybe_platform/2

    defp maybe_platform(queryable, nil), do: queryable
    defp maybe_platform(queryable, platform) do
      queryable
      |> where([c], c.platform == ^platform)
    end
    

    Then your query_clicks/2 looks like

    def query_clicks(freq \\ "day", platform \\ false) do
      from(Click)
      |> select(
        [c],
        [
          fragment("date_trunc(?,?) as t", ^freq, c.inserted_at), count(c.link_id) 
        ]
      )
      |> maybe_platform(platform)
      |> group_by([c], fragment("t"))
      |> Repo.all
    end
    

    It can be done in a anonymous function like you did but this is a little clearer imo.