Ecto Repo.one for each number in the range

Hello,
I want to do 12 queries for each month, reject nils from them and then return them to my controller and render JSON API Response, I am trying to do it like this:

def list_working_days(%{location: location, year: year}) do
    query_for_month = fn month ->
      from d in WorkingDay,
        inner_join: w in Workplace,
        on: w.id == d.work_place_id,
        where: d.month == ^month and d.year == ^year,
        order_by: st_distance(w.geo_location, ^location),
        limit: 1
    end

    1..12
    |> Enum.map(query_for_month)
    |> Enum.map(fn query -> Repo.one(query) end)
    |> Enum.reject(&is_nil/1)
  end

Somehow I get Argument error in the render function,
If I IO.Inspect(working_days) in my controller, I can see the list of days.
Does anyone know what is going on?

(exit) an exception was raised:
    ** (ArgumentError) argument error
(....... a lot of stacktrace, my working_days list actually)
(myapp 0.1.0) lib/myapp_web/views/api/v0/working_day_view.ex:14: MyAppWeb.Api.V0.WorkingDayView.render/2
        (elixir 1.12.3) lib/enum.ex:1582: Enum."-map/2-lists^map/1-0-"/2
        (myapp 0.1.0) lib/myapp_web/views/api/v0/working_day_view.ex:10: MyAppWeb.Api.V0.DegreeDayView.render/2

FYI: I’m new to the language, been a Junior RoR Developer before

It seems that something is off in the controller. Can you provide more of the stacktrace and a bit more of the controller code?

1 Like

Can you please post whats in your view code - working_day_view.ex
Seems like the error in in the view file.

1 Like

Actually I had to do a flat_map instead of map in the controller, Since I was invoking this function on a list of years.

Closed