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


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="script" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/script/120/8725_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  script
                    <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/eiji" rel="nofollow">@Eiji</a>  sorry for bothering you again and again. I found a case where it’s not returning correct output.</p>
<pre><code>       %{
       "antibiotic" =&gt; %{"$binding" =&gt; :first},
       "prescribing_clinician" =&gt; %{"$binding" =&gt; :first}
       }
</code></pre>
<p>the above code returning empty preload.</p>
<p>If I add this:</p>
<pre><code>      defp do_preloading({key, %{"$binding" =&gt; :first}}, _acc), do: String.to_atom(key)
</code></pre>
<p>It’s returning only one preload:</p>
<pre><code>    preload: [:prescribing_clinician]
</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="149105" 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/preload-tables-in-ecto-dynamically/26502/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-149105" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="149105"
                     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 #11"></div>
  </section>
</div>
    <div class="postbit" id="149110" data-post-id="149110">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p><a class="mention" href="/u/script" rel="nofollow">@script</a> I’m a bit lost or don’t remember something. Should not it be:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">%{
  "antibiotic" =&gt; %{"$binding" =&gt; :last},
  "prescribing_clinician" =&gt; %{"$binding" =&gt; :last}
}
</code></pre>
<p>If I remember it correct <code>$binding</code> set to <code>:last</code> should be a leaf and should not have <code>$include</code> inside it. If so then your input is invalid, right?</p>
<p>Also I made one small mistake with return (noticed when you gave me this line after changes):</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Example do
  def sample(map) when is_map(map), do: do_sample(map)

  defp do_sample(map), do: Enum.reduce(map, [], &amp;do_sample/2)

  defp do_sample({key, %{"$include" =&gt; include}}, acc) when is_map(include),
    do: [{String.to_atom(key), do_sample(include)} | acc]

  defp do_sample({key, %{"$include" =&gt; include}}, acc) when is_bitstring(include),
    do: [{String.to_atom(key), String.to_atom(include)} | acc]

  defp do_sample({key, %{"$include" =&gt; include}}, acc) when is_list(include),
    do: [{String.to_atom(key), Enum.map(include, &amp;String.to_atom/1)} | acc]

  # before I did not used `acc` here:
  defp do_sample({key, %{"$binding" =&gt; :last}}, acc), do: [String.to_atom(key) | acc]

  defp do_sample({_key, _value}, acc), do: acc
end
</code></pre>
<p>If we would call it with such input:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">data = %{
  "antibiotic" =&gt; %{"$binding" =&gt; :last},
  "prescribing_clinician" =&gt; %{"$binding" =&gt; :last}
}

data |&gt; Example.sample() |&gt; IO.inspect()
</code></pre>
<p>then it would return:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">[:prescribing_clinician, :antibiotic]
</code></pre>
<p>Again the better way is to create a test for all cases. Once you do it you should have all information you need.</p>
<p>Also looks like that you do not understand my code, so I would describe it:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Example do
  # Enum.reduce/3 would be used multiple times
  # this is function to work with only top level
  # so reduce which happens also nested is called in separate function
  # in order to allow easy change of code
  def sample(map) when is_map(map), do: do_sample(map)

  # this is used in `sample/1` and next clausule
  defp do_sample(map), do: Enum.reduce(map, [], &amp;do_sample/2)

  # checks if our value have map with `$include` as key
  # then we also check if `include` is map which is important for rest clausules
  defp do_sample({key, %{"$include" =&gt; include}}, acc) when is_map(include),
    do: [{String.to_atom(key), do_sample(include)} | acc]

  # checks if our value have map with `$include` as key
  # then we also check if `include` is string which is important for rest clausules
  defp do_sample({key, %{"$include" =&gt; include}}, acc) when is_bitstring(include),
    do: [{String.to_atom(key), String.to_atom(include)} | acc]

  # checks if our value have map with `$include` as key
  # then we also check if `include` is list
  # as you did not gave a example with list of maps
  # I made it this clausule working with strings only
  defp do_sample({key, %{"$include" =&gt; include}}, acc) when is_list(include),
    do: [{String.to_atom(key), Enum.map(include, &amp;String.to_atom/1)} | acc]

  # as described before if we have leaf then we do not go nested
  # this is only a small optimization in order to call another iterate again
  defp do_sample({key, %{"$binding" =&gt; :last}}, acc), do: [String.to_atom(key) | acc]

  # if nothing else matches then simply return acc
  # you can debug here which things does not match
  defp do_sample({_key, _value}, acc), do: acc
end
</code></pre>
<p>Also don’t worry in case of any questions. <img src="https://forum.elixirforum.com/images/emoji/apple/smiley.png?v=15" title=":smiley:" class="emoji" alt=":smiley:" 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="149110" 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/preload-tables-in-ecto-dynamically/26502/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-149110" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="149110"
                     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 #12"></div>
  </section>
</div>
    <div class="postbit" id="149111" data-post-id="149111">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="script" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/script/120/8725_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  script
                    <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>That explains it perfectly. Thank you again <a class="mention" href="/u/eiji" rel="nofollow">@Eiji</a>  for your time and explanation.</p>
<p>P.S: Any coding tips to write clean code as you do.<img src="https://forum.elixirforum.com/images/emoji/apple/grinning.png?v=15" title=":grinning:" class="emoji" alt=":grinning:" 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="149111" 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/preload-tables-in-ecto-dynamically/26502/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-149111" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="149111"
                     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 #13"></div>
  </section>
</div>
    <div class="postbit" id="149117" data-post-id="149117">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="script" data-post="14" data-topic="26502">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/script/48/8725_2.png" class="avatar"> script:</div>
<blockquote>
<p>P.S: Any coding tips to write clean code as you do.<img src="https://forum.elixirforum.com/images/emoji/apple/grinning.png?v=15" title=":grinning:" class="emoji" alt=":grinning:" loading="lazy" width="20" height="20"></p>
</blockquote>
</aside>
<p>In short? Of course: <strong>PRACTICE</strong> <img src="https://forum.elixirforum.com/uploads/default/original/2X/2/20c9c52366de24191270f0b11311379ce60ac82d.gif?v=15" title=":077:" class="emoji emoji-custom" alt=":077:" loading="lazy" width="20" height="20"></p>
<p>This topic may be interesting for you:</p>
<aside class="quote quote-modified" data-post="1" data-topic="96">
  <div class="title">
    <div class="quote-controls"></div>
    <img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/astonj/48/15_2.png" class="avatar">
    <div class="quote-title__text-content">
      <a href="https://forum.elixirforum.com/t/what-is-your-number-one-tip-for-anyone-new-to-elixir/96" rel="nofollow">What is your number one tip for anyone new to Elixir?</a> <a class="badge-category__wrapper " href="/c/learning-resources/guides-tuts-tips-info/61" rel="nofollow"><span data-category-id="61" style="--category-badge-color: #3AB54A; --category-badge-text-color: #FFFFFF; --parent-category-badge-color: #3AB54A;" data-parent-category-id="8" data-drop-close="true" class="badge-category --style-square --has-parent" title="Tutorials &amp; Guides Section"><span class="badge-category__name">Guides/Tuts/Tips/Info</span></span></a>
    </div>
  </div>
  <blockquote>
    From me, it’s to learn about the equals sign = first. It isn’t the assignment operator as you might expect! 
It can do assignment, but it can do a lot more too! Think of it more like the equals sign you learned about in your maths lessons - where the formula on both sides actually match by ending up being equal, for example, a = b + 3 if you know the value of a you can work out the value of b, and if you know the value of b you can work out the value of a. This allows you to do stuff with the va…
  </blockquote>
</aside>

<p>Personally this quote is probably one of my best (for this specific question):</p>
<aside class="quote no-group" data-username="Eiji" data-post="3" data-topic="26041">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/eiji/48/36743_2.png" class="avatar"><a href="https://forum.elixirforum.com/t/when-starting-a-project-from-scratch-do-you-build-back-end-or-front-end-first/26041/3" rel="nofollow">When starting a project from scratch, do you build back-end or front-end first?</a></div>
<blockquote>
<p>It’s also a bit similar to question: <code>What is a good way to learn Elixir?</code> . Basically you learn in your own way and answers are probably boring, but always to the point i.e. <strong>practice</strong> . Here it looks exactly same, but instead typical tip would be something like <strong>ask yourself</strong> . Well … at end everything goes into <strong>practice</strong> , so even if you would follow not the best way then by simply practicing you would slowly see what you can improve. Once you notice them you just need to ask how to improve and finally again at end you would have <strong>practice</strong> again. <img src="https://forum.elixirforum.com/images/emoji/apple/smile.png?v=15" title=":smile:" class="emoji" alt=":smile:" loading="lazy" width="20" height="20"></p>
<p>You may be confused and ask why, but the answer is simple … Every human is learning to the end of life and learning means … <strong>PRACTICE</strong> - it’s ours Rome <img src="https://forum.elixirforum.com/uploads/default/original/2X/2/20c9c52366de24191270f0b11311379ce60ac82d.gif?v=15" title=":077:" class="emoji emoji-custom" alt=":077:" loading="lazy" width="20" height="20"></p>
</blockquote>
</aside> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="149117" 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/preload-tables-in-ecto-dynamically/26502/15">Post #14</a>
	                </div>
	            </div>
              <div id="likers-container-149117" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="149117"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-last-post cat-last-post" title="Last post!"></div>
  </section>
</div>
</template></turbo-stream><turbo-stream action="replace" target="load-more-container"><template><div id="load-more-container" class="load-more-container">
    <span class="all-loaded">— All posts loaded —</span>
</div></template></turbo-stream>