david234
Elixir - increment a field value in Ecto map
I am getting two maps to a function.
Map1 is a list of maps
Map2 is a single map
I have to Enum.each this Map1 and marge with Map2(Which is static) and then insert in to database
This is working fine for me
Map example before inserting in to Database
%{
api_id: 1,
category_id: 2,
content_url: "SeC9UPNoND8",
sub_category_id: 1
}
%{
api_id: 1,
category_id: 2,
content_url: "4wb8AYt3ECM",
sub_category_id: 1
}
%{
api_id: 1,
category_id: 2,
content_url: "2TSdkYkapo8",
sub_category_id: 1
}
def store_in_db(Map1, Map2) do
Enum.each(Map1, fn(data) ->
Map.merge(data, Map2)
|> Contents.create_content
end)
end
But now i got one new field to be added which is an just an increment $i++. which must be stored in priority field. For example in Map1 list i have got 3 maps. So i need below output
%{
api_id: 1,
category_id: 2,
content_url: "SeC9UPNoND8",
priority: 1,
sub_category_id: 1
}
%{
api_id: 1,
category_id: 2,
content_url: "4wb8AYt3ECM",
priority: 2,
sub_category_id: 1
}
%{
api_id: 1,
category_id: 2,
content_url: "2TSdkYkapo8",
priority: 3,
sub_category_id: 1
}
I have done this
def store_in_db(Map1, Map2) do
Enum.each(Map1, fn(data) ->
Map.merge(data, Map2)
|> Map.merge(%{priority: 1})
|> Contents.create_content
end)
end
In that i want to increment priority value. Can someone give me insight on how to achieve this
Marked As Solved
kokolegorille
Not tested, but something like this…
BTW Did You name your variables from Erlang? It’s usual to start with lowercase in Elixir.
def store_in_db(m1, m2) do
m1
|> Enum.with_index()
|> Enum.each(fn({data, index}) ->
Map.merge(data, m2)
|> Map.merge(%{priority: index})
|> Contents.create_content
end)
end
2
Also Liked
kokolegorille
You could also specify the offset as a parameter…
|> Enum.with_index(1)
3
Popular in Questions
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
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
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
Hello all!
I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
As a follow up to my earlier question:
I have the code compiling and running but not getting a successful login from the rest server. ...
New
I would like to know what is the best IDE for elixir development?
New
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
Hi everyone!
I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
Other popular topics
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
Manning 2016 Halloween weekend sale via Deal of the Day
Friday, October 28 - Half off all MEAPs - code WM102816LT
Saturday, October 29 ...
New
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!)
This post collects co...
New
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
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
i’m a new one to elixir
which editor can i use
vs code? or atom?
Thanks! :smiley:
New
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
Hello guys,
I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New







