jamesmintram

jamesmintram

Phoenix form many_to_many

So I am trying to figure out how to work with Forms + Many to many relationships.

Lets say I have the following

schema "tour" do
    field :name, :string
    many_to_many(
      :city,
      City,
      join_through: "tour_cities",
      on_replace: :delete
    )
end

schema "city" do
    field :name, :string
end

And lets assume my tour_controller->create method
receives a params map like the following:

%{“city” => 1, “name” => “Best tour in the world”}

City is supplied via a dropdown in the form the
contents populated via a query on the City table.
1 is the ID of the city I want to relate to the tour I am about to create.

How do I make this work from the perspective of the
controller/changeset/whatever?

I have experimented with put_assoc, but that only
seems to work with a fully loaded Changeset. I
would ideally like to not have to hit the database
again to retrieve full changesets - when all I really
need is the ID of the City.

What I have made work:

put_assoc(:cities, new_tour, [%City{...fully loaded}, %City{...fully loaded}])

What doesn’t work:

put_assoc(:cities, new_tour, [%City{id: 2}, %City{id: 4}])

This example is simplified, eventually I want to
present the user with the option of adding multiple
cities at once in the form. But before I get to that,
I am trying to keep the form simple while I get the
many to many relationship working.

Marked As Solved

kokolegorille

kokolegorille

For many to many I like to follow this post…

Where Next?

Popular in Questions Top

_russellb
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
albydarned
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
fireproofsocks
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
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call t...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
marick
I had some trouble figuring out how to make many-to-many associations work. Once I got it working, I wrote a blog post. Because I’m a nov...
New

Other popular topics 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
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 53690 245
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
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
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36128 110
New
boundedvariable
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
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