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


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="josevalim" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/josevalim/120/1787_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  josevalim
                  </h3>
		          </div>
						
			          <div class="user-title">
									<span>Creator of Elixir</span>
			          </div>
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Such clarifications to <code>dynamic/2</code> docs would be really welcome too. <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="86484" data-batch-url="/posts/batch_likers">
                        3
                      </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/dynamic-queries-in-ecto/14858/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-86484" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="86484"
                     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="92595" data-post-id="92595">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="peerreynders" data-post="11" data-topic="14858">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/peerreynders/48/5826_2.png" class="avatar"> peerreynders:</div>
<blockquote>
<p>Ecto queries are composable. <a href="https://hexdocs.pm/ecto/Ecto.Query.html#dynamic/2" rel="noopener nofollow ugc"> <code>Ecto.Query.dynamic/2</code> </a> comes in handy when you need your <code>where</code> conditions to be composable.</p>
<p>Conditions are composed with <code>and</code> or <code>or</code> :</p>
<ul>
<li>when you compose with <code>or</code> and you don’t need the condition, you default to <code>false</code> instead.</li>
<li>when you compose with <code>and</code> and you don’t need the condition, you default to <code>true</code> instead.</li>
</ul>
<p>Furthermore if you are simply <code>and</code> ing the conditions you need, you probably don’t need <code>Ecto.Query.dynamic/2</code> :</p>
</blockquote>
</aside>
<p>This passage is very informative. Assuming that it is accurate, it should be part of the dynamic/2 section in the docs. Also, the general advice (part of the docs) that “one should never need to invoke [Query.API functions] directly” seems to be misleading unless the ubiquity of field/2 contravenes best practice.</p>
<p>(This is my first post, I hope that this is the right place for stimulating improvements in the documentation.)</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="92595" 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/dynamic-queries-in-ecto/14858/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-92595" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="92595"
                     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="92597" data-post-id="92597">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Ecto is open source. You could add those parts yourself and submit a pull request on github.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="92597" 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/dynamic-queries-in-ecto/14858/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-92597" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="92597"
                     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="173828" data-post-id="173828">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Hello people,</p>
<p>I started learning Elixir and was trying to create a dynamic filter to send an object with non-mandatory properties, so I came up with this solution.</p>
<p>I hope it helps.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">def search filter do

    query = from user in User, where: 1==1

    filterByName = fn (query, filter) -&gt;
      unless is_nil Map.get(filter, :name) do
        from(user in query, where: user.name == ^filter.name)
      else
        query
      end
    end

    filterByEmail = fn (query, filter) -&gt;
      unless is_nil Map.get(filter, :email) do
        from(user in query, where: user.email == ^filter.email)
      else
        query
      end
    end

    filterByBirth = fn (query, filter) -&gt;
      unless is_nil Map.get(filter, :birth) do
        from(user in query, where: user.birth == ^filter.birth)
      else
        query
      end
    end

    query = filterByName.(query, filter)
    query = filterByEmail.(query, filter)
    query = filterByBirth.(query, filter)

    Repo.all query
  end
</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="173828" 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/dynamic-queries-in-ecto/14858/15">Post #14</a>
	                </div>
	            </div>
              <div id="likers-container-173828" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="173828"
                     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>