<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="82176" data-post-id="82176">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="Almog" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/Almog/120/9354_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  Almog
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Thanks did not see your example, so I think I finally understand how this should work. I updated everything which makes sense but I’m getting the following error when I got the add new magnet view</p>
<p>function Magnify.Magnets.change_magnet/1 is undefined (module Magnify.Magnets is not available)</p>
<p>I have this is my controller</p>
<p>alias Magnify.Magnets<br>
alias Magnify.Magnets.Magnet<br>
alias Magnify.Repo</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="82176" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/cast-assoc-is-null/14280/22">Post #21</a>
	                </div>
	            </div>
              <div id="likers-container-82176" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="82176"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #21"></div>
  </section>
</div>
    <div class="postbit" id="82177" data-post-id="82177">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="Almog" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/Almog/120/9354_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  Almog
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Never I think I got it working, <img src="https://forum.elixirforum.com/images/emoji/apple/slight_smile.png?v=15" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="82177" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/cast-assoc-is-null/14280/23">Post #22</a>
	                </div>
	            </div>
              <div id="likers-container-82177" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="82177"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #22"></div>
  </section>
</div>
    <div class="postbit" id="82178" data-post-id="82178">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="Almog" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/Almog/120/9354_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  Almog
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I’m getting</p>
<ul>
<li>(CompileError) lib/magnify/magnets/magnets.ex:54: User.<strong>struct</strong>/0 is undefined, cannot expand struct User</li>
</ul>
<p>Do I need to alias the user?</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="82178" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/cast-assoc-is-null/14280/24">Post #23</a>
	                </div>
	            </div>
              <div id="likers-container-82178" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="82178"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #23"></div>
  </section>
</div>
    <div class="postbit" id="82179" data-post-id="82179">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="Almog" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/Almog/120/9354_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  Almog
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Ok so I did this</p>
<pre><code>def create_magnet(attrs, for: %Magnify.Coherence.User{id: user_id}) do
    %Magnet{user_id: user_id}
    |&gt; Magnet.changeset(attrs)
    |&gt; Repo.insert()
  end
</code></pre>
<p>which is fine now, when create the object or post I get</p>
<p>Postgrex expected a binary of 16 bytes, got 1. Please make sure the value you are passing matches the definition in your table or in your query or convert the value accordingly.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="82179" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/cast-assoc-is-null/14280/25">Post #24</a>
	                </div>
	            </div>
              <div id="likers-container-82179" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="82179"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #24"></div>
  </section>
</div>
    <div class="postbit" id="82180" data-post-id="82180">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="dokuzbir" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  dokuzbir
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group quote-modified" data-username="idi527" data-post="20" data-topic="14280">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/i/85e7bf/48.png" class="avatar"> idi527:</div>
<blockquote>
<p>You might want to warn them about it. As I’ve mentioned before, this approach (casting foreign keys inside the changeset functions) allowed me to gain write access to any record in one shop’s database.</p>
</blockquote>
</aside>
<p>I think most of elixir community vulnerable. Really thanks i see always cast_assocs or id in changesets. This is the first time i see your approach. But on the other hand we already modify user_id from controller so somebody cant put another user_id right ?</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="82180" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/cast-assoc-is-null/14280/26">Post #25</a>
	                </div>
	            </div>
              <div id="likers-container-82180" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="82180"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #25"></div>
  </section>
</div>
    <div class="postbit" id="82182" data-post-id="82182">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="Almog" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/Almog/120/9354_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  Almog
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Thanks everyone, got it working, have to the community around Elixir is just amazing. Now to the next challenge / task</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="82182" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/cast-assoc-is-null/14280/27">Post #26</a>
	                </div>
	            </div>
              <div id="likers-container-82182" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="82182"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #26"></div>
  </section>
</div>
    <div class="postbit" id="82184" data-post-id="82184">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="idi527" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  idi527
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="dokuzbir" data-post="26" data-topic="14280">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/d/ed8c4c/48.png" class="avatar"> dokuzbir:</div>
<blockquote>
<p>But on the other hand we already modify user_id from controller so somebody cant put another user_id right ?</p>
</blockquote>
</aside>
<p>In your example above, yes. Since you use <code>Map.put(params, "user_id", ...)</code> it replaces the <code>"user_id"</code> provided in the params.</p>
<p>But what if you or someone else who starts working on your codebase forgets to use <code>Map.put/3</code> or to delete the <code>"user_id"</code> key from <code>params</code>, or uses <code>Map.put_new/3</code> in some other controller using the same changeset function (which casts <code>user_id</code>), or replaces <code>:user_id</code> with <code>:owner_id</code> in the schema but forgets to update <code>Map.put(params, "user_id", ...)</code> with <code>Map.put(params, "owner_id", ...)</code> in the controller?</p>
<p>Then the <code>"user_id"</code> from <code>params</code> leaks into the changeset function and you have a vulnerability. Any user can send whatever <code>"user_id"</code> they want thus completely bypassing any authentication logic you have.</p>
<p>The only way to avoid it, as I see it, is to not cast <code>user_id</code> (or any other foreign key) inside the changeset functions but put it into the schema structs explicitly when needed.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="82184" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/cast-assoc-is-null/14280/28">Post #27</a>
	                </div>
	            </div>
              <div id="likers-container-82184" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="82184"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #27"></div>
  </section>
</div>
    <div class="postbit" id="82188" data-post-id="82188">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="Almog" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/Almog/120/9354_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  Almog
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p><a class="mention" href="/u/idi527" rel="nofollow">@idi527</a> would you do the same thing for many_to_many</p>
<p>I have</p>
<pre><code>schema "fluxes" do
    field :description, :string
    field :title, :string
    many_to_many :magnets, Magnify.Magnets.Magnet, join_through: "magnets_fluxes"

    timestamps()
  end

  @doc false
  def changeset(flux, attrs) do
    flux
    |&gt; cast(attrs, [:title, :description])
    |&gt; validate_required([:title, :description])
  end 
</code></pre>
<p>And not sure the correct to add magnets use puts_assoc but not sure?</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="82188" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/cast-assoc-is-null/14280/29">Post #28</a>
	                </div>
	            </div>
              <div id="likers-container-82188" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="82188"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #28"></div>
  </section>
</div>
    <div class="postbit" id="82189" data-post-id="82189">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="joaquinalcerro" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/joaquinalcerro/120/13928_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  joaquinalcerro
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I posted about this topic a few days ago:</p>
<aside class="quote quote-modified" data-post="1" data-topic="13684">
  <div class="title">
    <div class="quote-controls"></div>
    <img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/joaquinalcerro/48/13928_2.png" class="avatar">
    <div class="quote-title__text-content">
      <a href="https://forum.elixirforum.com/t/handling-nested-associations-with-cast-assoc/13684" rel="nofollow">Handling nested associations with cast_assoc</a> <a class="badge-category__wrapper " href="/c/questions-help/questions/53" rel="nofollow"><span data-category-id="53" style="--category-badge-color: #C14BFB; --category-badge-text-color: #000000; --parent-category-badge-color: #C14BFB;" data-parent-category-id="171" data-drop-close="true" class="badge-category --style-square --has-parent" title="Elixir Questions / Help"><span class="badge-category__name">Questions</span></span></a>
    </div>
  </div>
  <blockquote>
    Hi everyone, 
I finally had a bit of time to followup on this post: <a href="https://forum.elixirforum.com/t/best-way-to-handle-associations-in-single-html-form/12898" class="inline-onebox-loading" rel="nofollow">https://forum.elixirforum.com/t/best-way-to-handle-associations-in-single-html-form/12898</a> 
In my previous post I just wanted to create a handle a nested association in the same html form. Specifically to create the parent and child in one go. 
In this post I want to go a little bit further and try to explain to new comers how I manage to implement a full CRUD for this scenarios. 
Please feel free to comment in this post how did …
  </blockquote>
</aside>

<aside class="quote quote-modified" data-post="1" data-topic="12898">
  <div class="title">
    <div class="quote-controls"></div>
    <img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/joaquinalcerro/48/13928_2.png" class="avatar">
    <div class="quote-title__text-content">
      <a href="https://forum.elixirforum.com/t/best-way-to-handle-associations-in-single-html-form/12898" rel="nofollow">Best way to handle associations in single html form</a> <a class="badge-category__wrapper " href="/c/questions-help/questions/53" rel="nofollow"><span data-category-id="53" style="--category-badge-color: #C14BFB; --category-badge-text-color: #000000; --parent-category-badge-color: #C14BFB;" data-parent-category-id="171" data-drop-close="true" class="badge-category --style-square --has-parent" title="Elixir Questions / Help"><span class="badge-category__name">Questions</span></span></a>
    </div>
  </div>
  <blockquote>
    Hi everyone, 
I have been learning a bit more on Ecto and Phoenix. Specifically in how to handle a master/detail o parent/child associations and the corresponding Web Form. 
My question: Is there a better way to handle this typical scenario than the one I implemented below? 
In my journey I read about: 

Ecto associations, multi, cast_assoc, put_assoc, embeded_schemas, transactions
Drab Library

Checktout many resources like: 

Phoenix, Ecto, Drab official documentation
Different Forums: Medium,…
  </blockquote>
</aside>

<p>The scenario might be similar but keep in mind that using cast_assoc requires you to handle the parent and child at the same time.</p>
<p>Hope this helps.</p>
<p>Best regards,</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="82189" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/cast-assoc-is-null/14280/30">Post #29</a>
	                </div>
	            </div>
              <div id="likers-container-82189" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="82189"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #29"></div>
  </section>
</div>
    <div class="postbit" id="82193" data-post-id="82193">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="idi527" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  idi527
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I’d probably use an <a href="https://hexdocs.pm/ecto/Ecto.Multi.html" rel="noopener nofollow ugc">ecto multi</a> to insert the flux and create relationships between it and some magnets inside a single database transaction.</p>
<p>Adapted from <a href="https://forum.elixirforum.com/t/is-there-are-way-to-bulk-insert-join-records-of-a-many-to-many-relationship-with-ecto/13630/3" class="inline-onebox" rel="nofollow">Is there are way to bulk insert join records of a many-to-many relationship with Ecto? - #3 by OvermindDL1</a></p>
<pre data-code-wrap="elixir"><code class="lang-elixir"># in the module containing your business logic

# here I suppose that the magnets already exist
# and we only need to associate them with the 
# created flux
@spec create_flux(%{(String.t | atom) =&gt; term}, [magnet_id :: pos_integer]) :: {:ok, %Flux{}} | {:error, Ecto.Changeset.t}
def create_flux(flux_attrs, magnet_ids) do
  alias Ecto.Multi

  flux_changeset = Flux.changeset(%Flux{}, flux_attrs)

  Multi.new()
  |&gt; Multi.insert(:flux, flux_changeset)
  |&gt; Multi.run(:magnet_relationships, fn %{flux: %Flux{id: flux_id}} -&gt;
    # prepare relationships to be inserted into the many-to-many table
    relationships = Enum.map(magnet_ids, fn magnet_id -&gt;
      %{magnet_id: magnet_id, flux_id: flux_id}
    end)
    
    # https://hexdocs.pm/ecto/Ecto.Repo.html#c:insert_all/3
    {:ok, Repo.insert_all("magnets_fluxes", relationships)}
  end)
  |&gt; Repo.transaction()
  |&gt; case do
    {:ok, %{flux: flux}} -&gt; {:ok, flux}
    {:error, :flux, changeset, _changes} -&gt; {:error, changeset}
  end
end
</code></pre>
<p>Used like this</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">flux_attrs = %{title: "a flux", description: "a flux"}
magnet_ids = [1, 2, 3] # I suppose these already exist in the database

create_flux(flux_attrs, magnet_ids)
</code></pre> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="82193" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/cast-assoc-is-null/14280/31">Post #30</a>
	                </div>
	            </div>
              <div id="likers-container-82193" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="82193"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #30"></div>
  </section>
</div>
</template></turbo-stream><turbo-stream action="replace" target="load-more-container"><template><div id="load-more-container" class="load-more-container">
    <a class="load-more-button" data-turbo-stream="true" href="/topics/14280/load_more?page=4">Load more posts (19 remaining)</a>
</div></template></turbo-stream>