Hey, Thank you soo much for your time to guide me regarding this. It really means a lot.
Here’s the new changes as you mentioned that I must remove the extra or wasted code. It works, but still not getting the values in the map from File.changeset(file), that’s why my after_save isn’t working on the values provided by the changeset.
Here’s the new code:
def create_files(files \\ [], after_save \\ &{:ok, &1}) do
allFiles =
for file <- files do
%File{}
|> File.changeset(file)
|> after_save(after_save)
|> Repo.insert()
end
IO.inspect(allFiles)
# my_files = {allFiles, []}
{:ok, files}
end
defp after_save({:ok, file}, func) do
{:ok, _file} = func.(file)
end
defp after_save(error, _func) do
error
end
The shape of values given by changeset in the output is:
[
#Ecto.Changeset<
action: nil,
changes: %{
name: "calm.jpg",
size: 49472,
type: "image/jpeg",
url: "/uploads/6066ee3d-815f-4e93-812d-3b1292b72959.jpg",
user_id: 1
},
errors: [],
data: #Initium.Files.File<>,
valid?: true
>,
#Ecto.Changeset<
action: nil,
changes: %{
name: "bgrdg.jpg",
size: 42531,
type: "image/jpeg",
url: "/uploads/4752565d-d0ea-456c-b583-88e9d5adcf65.jpg",
user_id: 1
},
errors: [],
data: #Initium.Files.File<>,
valid?: true
>
]
Here in the code we can see that the returning values aren’t in a shape of a map. The changeset is not returning the map.
Thanks