sharkmyster

sharkmyster

How to perform bulk associations with ecto?

I’m working on a game app that has an answers table and a categories table. This relationship is many to many.

The admins for the app can add answers and categories individually using usual controller format. However, I want to create a way to bulk associate answers to categories. On a category page there is a textarea to receive a comma separated list of strings. Upon hitting submit, I want to achieve the following steps,

  • assess if any of the strings already exist as an answer
  • for any that do not exist, create an record in the answers table
  • for all the answers that were given, create the association in the join table

Here is my attempt.

  def put_answers_to_category(category, answers_string) do
    answers =
      answers_string
      |> parse_answers_string()
      |> find_existing_and_create_new()

    old_category =
      category
      |> Repo.preload(:answers)
      |> IO.inspect()

    category
    |> Ecto.Changeset.change()
    |> Ecto.Changeset.put_assoc(:answers, Enum.uniq(answers ++ old_category.answers))
    |> IO.inspect()
    |> Repo.update()
  end

  def parse_answers_string(answers_string) do
    answers_string
    |> String.split(",", trim: true)
    |> Enum.map(&String.trim/1)
    |> Enum.reject(&(&1 == ""))
  end

  def find_existing_and_create_new(answers_list) do
    existing_answers =
      Answer
      |> where([a], a.raw in ^answers_list)
      |> Repo.all()

    existing_answers_raw = Enum.map(existing_answers, & &1.raw)

    new_answers =
      answers_list
      |> Enum.filter(fn ans -> !Enum.member?(existing_answers_raw, ans) end)
      |> Enum.map(fn ans -> %{raw: ans} end)
      |> Enum.map(&create_answer/1)
      |> Enum.map(fn {:ok, answer} -> answer end)

    existing_answers ++ new_answers
  end

This seems to be working but I think it may be verbose/inefficient. I’ve read the docs around cast_assoc and put_assoc but I couldn’t quite find a better way. Is there a better/more efficient way to do this?

Marked As Solved

tfwright

tfwright

As far as Ecto goes, I think that your implementation is the proper way to approach updating a many_to_many. But that really just means using put_assoc with an array of the set of answer records you want the category changeset to have.

The rest of the complexity here comes from the bulk upsert on the answer text param, but changing that would require a different API design. For example, a more conventional many_to_many update would take the answer ids directly, requiring them to all already exist. For example, the FE could be upserting them individually as they are entered via another endpoint.

Don’t know enough about your requirements to comment on whether that would be better overall, but I would say that you lose a significant advantage of your current approach by creating the new answers in a separate DB transaction (AFAICT), which means if the category update never goes through for whatever reason (validation error) the new answers will still be around in your DB, which may be unexpected.

Where Next?

Popular in Questions Top

marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
JorisKok
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
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 43622 214
New
belgoros
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
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New

We're in Beta

About us Mission Statement