TheYuwana

TheYuwana

Ecto join and order_by with dynamic values

Hey fellow Elixirians!

I am having trouble with joining and ordering, but with dynamic values. So basically I send a dynamic “asc” or “desc” and a dynamic field to the function and it needs to order according to whats given. The problem is, that you can’t really put dynamic values in the ecto queries… Or maybe I am doing it wrong. But in any case, here is my code:

def with_ordering(query, filters, orderable_fields) do
if Map.has_key?(filters, "order") and filters["order"] in @order_types do
  order_by =
    if Map.has_key?(filters, "order_by") and filters["order_by"] in orderable_fields do
      String.to_atom(filters["order_by"])
    else
      :inserted_at
    end

  from(
    ue in query,
    join: e in assoc(ue, :event)
    order_by: ["#{filters["order"]}": ^order_by]
  )
else
  query
end

end

Next to not being able to do certain things in the query, how can I order_by “e” with the dynamic value?

I am trying to achieve the following results dynamically:

from(
    ue in query,
    join: e in assoc(ue, :event)
    order_by: [asc: e.id]
  )

Any tips, best practices, tricks and hacks are appreciated!

Marked As Solved

LostKobrakai

LostKobrakai

Doesn’t …, order_by: [{^direction, field(e, ^field)}] work?

10
Post #6

Also Liked

TheYuwana

TheYuwana

@LostKobrakai Yes! This is it! Strange that this way of writing it is not in the docs for order_by. Would be great to note that, unless it’s written somewhere else I don’t know of… But in any case, thanks!

Here is the final result and solution (according to my case):

order_field = String.to_atom(filters["order_by"])
direction = String.to_atom(filters["order"])
  from(
      ue in query,
      join: e in assoc(ue, :event),
      order_by: [{^direction, field(e, ^order_field)}]
    )
LostKobrakai

LostKobrakai

https://hexdocs.pm/ecto/2.2.10/Ecto.Query.API.html#field/2
This is probably what you’re looking for.

TheYuwana

TheYuwana

@mbuhot Have tried that already, but there is no way I can acces the joined table field. Like this will not work:

values = [asc: field(c, :name)]
from(
    e in query,
    join: c in assoc(e, :city)
    order_by: ^values
  )

Where Next?

Popular in Questions Top

Tee
can someone please explain to me how Enum.reduce works with maps
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
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
Kagamiiiii
Student & New to elixir. Nice language. I want to convert a english character, e.g. “a”, which is stored in a variable, to it’s asci...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
kostonstyle
Hi all I want to have a unix time, from the current time plus 1 hour. DateTime.now + 1 hour How to get it in elixir? Thanks
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New

Other popular topics Top

JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1140 51847 244
New
William
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
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
alice
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
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New

We're in Beta

About us Mission Statement