I wrote this simple line code while the code was running in a development environment.
r variable is just an Ecto schema retrieved by a query, I just wanted to get the value of the field offers (a lis that contains another Ecto schema) and just sort it by updated_date.
And this works fine… in development.
r |> Map.get(:offers) |> Enum.sort_by(&(&1.updated_at), NaiveDateTime)
When I build the project in a production environment an exception is raised in that line code returning this error.
** (exit) an exception was raised:
** (BadFunctionError) expected a function, got: NaiveDateTime
(elixir) lib/enum.ex:2354: anonymous fn/3 in Enum.sort_by/3
(stdlib) lists.erl:969: :lists.sort/2
(elixir) lib/enum.ex:2354: Enum.sort_by/3
This is weird because I don’t find any problem with that code. I tried with other solutions but the same error happens.
r |> Map.get(:offers, []) |> Enum.sort_by(&(&1.updated_at), {:asc, NaiveDateTime})
r |> Map.get(:offers, []) |> Enum.sort_by(fn o -> o.updated_at end, NaiveDateTime)
r |> Map.get(:offers, []) |> Enum.sort_by(fn o -> o.updated_at end, {:asc, NaiveDateTime})
I’m running out of ideas, you guys know why this error happens?