Hey there,
In my code I want to return the value of changeset in the form of map or tuple to after_save. But the return value of changesets is not implementing on it. When i put this after_save after the Repo.insert it does work because the insertion is returning the value in the form of list. I want my after_save before insertion. But it does not seems to be possible because changesets is implementing the enumerable on the list.
def create_files(files \\ [], after_save \\ [&{:ok, &1}]) do
for file <- files do
changeset =
File.changeset(
%File{},
file
)
changeset
|> after_save(after_save)
|> Repo.insert()
IO.inspect(changeset)
end
{:ok, files}
end
It looks like you are defining your after_save function as a list of functions. You should either drop the brackets or iterate through the list of functions, calling each one individually.
However, one thing that I think seems to be off (please correct me if this is not the intended function) but you’re calling the after_save function before the insert - I don’t think Repo.insert accepts a {:ok, changeset} tuple. If you want to call the function after inserting it, swap the order of those two lines.
I wasn’t able to make a small test file to try out this code, but looking at the docs I think this is the case. Let me know how it goes
Hey @gus
Sorry, I was out. Thanks for your kind response. Yes you’re absolutely right. After swaping these two lines it works.
But my basic problem which I’m facing from a long while is that when I call after_save function before Repo.insert then the message of {:ok, files} recieves and redirected the page.
But on the same spot when i call my after_save after Repo.insert then it does work and saves every uploaded image thumbnail but neither redirect to another page nor sends {:ok, files} message.