I am trying to retrieve all work_items from the database and then add a calculated value to each of the work_items. I am stuck with this (that is not working):
def index(conn, _params) do
work_items = Tracking.list_work_items()
work_items =
work_items
for work_item <- work_items do
value = work_item_value(work_item)
work_item =
work_item
|> Map.put(:value, value)
end
render(conn, "index.html", work_items: work_items)
end
def work_item_value(work_item) do
work_item.duration_in_minutes/60 * work_item.hourly_rate_in_cents
end
work_items =
for work_item <- work_items do
value = work_item_value(work_item)
Map.put(work_item, :value, value)
end
render(conn, "index.html", work_items: work_items)
Probably not the most efficient way to do it. This should be a query. I like @Laetitia solution.
Thanks Laetitia! It appears that the code is not correct though. Even directly copying it I get 2 errors:
1:
** (Ecto.Query.CompileError) `work_item_value(work_item)` is not a valid query expression.
* If you intended to call a database function, please check the documentation
for Ecto.Query to see the supported database expressions
* If you intended to call an Elixir function or introduce a value,
you need to explicitly interpolate it with ^
2: When I fixed the first one (adding a ^ infront of the function):
warning: variable "work_item" does not exist and is being expanded to "work_item()", please use parentheses to remove the ambiguity or change the variable name
lib/fourty/tracking.ex:22: Fourty.Tracking.list_work_items/0
I read the documentation, but my programming skills are not developed enough yet. Any more help?