<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="241315" data-post-id="241315">
  <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">
								<aside class="quote no-group" data-username="silverdr" data-post="11" data-topic="45474">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/silverdr/48/23771_2.png" class="avatar"> silverdr:</div>
<blockquote>
<p>The first thing I don’t want to do is I don’t want to query the fields I am not interested in. For that I could use <code>select</code> and since on top of that, in the given case I am interested only in values then <code>pluck</code> gives me both in one go <img src="https://forum.elixirforum.com/images/emoji/apple/wink.png?v=15" title=":wink:" class="emoji" alt=":wink:" loading="lazy" width="20" height="20"></p>
</blockquote>
</aside>
<p><code>select</code> can do it in “one go” as well.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">user |&gt; Ecto.assoc(:chatrooms) |&gt; select([cr], map(cr, [:id, :name])) |&gt; Repo.all()
</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="241315" 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/has-many-through-load-selected-associated-columns/45474/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-241315" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="241315"
                     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="241317" data-post-id="241317">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="silverdr" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/silverdr/120/23771_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  silverdr
                    <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>Yes, as mentioned I used your initial response to create a helper:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">	def names_and_ids_for_user(user, order_by \\ nil) do
		user
		|&gt; Ecto.assoc(:chatrooms)
		|&gt; select([o], map(o, [:id, :name0]))
		|&gt; order_by(^order_by)
		|&gt; Repo.all()
	end
</code></pre>
<p>Which is exactly based on your suggestion. Once more - thank you for your prompt and very helpful responses!</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="241317" 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/has-many-through-load-selected-associated-columns/45474/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-241317" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="241317"
                     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="241327" data-post-id="241327">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I went a little too far with my answer by extracting it to a function when obviously <a class="mention" href="/u/lostkobrakai" rel="nofollow">@LostKobrakai</a> 's a way better version.  My point was just that Ecto is much faster than ActiveRecord (when you are creating AR objects) so it’s not the end of the world to grab all the fields but that is really neither here nor there.</p>
<p>Moving on from <code>pluck</code>, from what you wrote I get the impression you haven’t grasped the full power of composability.  You certainly shouldn’t be needing to write SQL first to turn it into Ecto with any regularity.  I’ve found Ecto to be incredibly concise and I don’t miss AR even a little.  A lot of reusable little one line query helpers can go a really long way.  I would have to see examples of what you’re doing, though (not suggesting you want to share, of course <img src="https://forum.elixirforum.com/images/emoji/apple/sweat_smile.png?v=15" title=":sweat_smile:" class="emoji" alt=":sweat_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="241327" 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/has-many-through-load-selected-associated-columns/45474/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-241327" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="241327"
                     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="241966" data-post-id="241966">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="silverdr" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/silverdr/120/23771_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  silverdr
                    <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">
								<aside class="quote no-group" data-username="sodapopcan" data-post="14" data-topic="45474">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/sodapopcan/48/34668_2.png" class="avatar"> sodapopcan:</div>
<blockquote>
<p>My point was just that Ecto is much faster than ActiveRecord (when you are creating AR objects)</p>
</blockquote>
</aside>
<p>Sure, Ecto is <em>expected</em> to be much faster in given case as it does not need to create large objects in a still relatively slow language/environment. I presume (and hope - although haven’t done any Benchee stuff to prove it) it should be faster even against the <code>pluck</code> option of Rails.</p>
<aside class="quote no-group" data-username="sodapopcan" data-post="14" data-topic="45474">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/sodapopcan/48/34668_2.png" class="avatar"> sodapopcan:</div>
<blockquote>
<p>so it’s not the end of the world to grab all the fields but that is really neither here nor there.</p>
</blockquote>
</aside>
<p>I am not sure what is the meaning of the end of the sentence here (?).</p>
<p>Still I very, very rarely “grab all the fields/columns” when I don’t need them. That’s because I very rarely am fully convinced that the table I grab things from today is not going to expand beyond the realm of what I find “decent” for this approach if not tomorrow then a bit later. Sure, for small projects, low traffic and stuff that is not really meant to be extended that is an absolutely viable way. In more general case OTOH I am much more constrained in my approach choices. Some may call it “premature optimisation”, which it might actually be, but I call it “it doesn’t cost me to do it more performant already from the start”. At least unless it does… <img src="https://forum.elixirforum.com/images/emoji/apple/wink.png?v=15" title=":wink:" class="emoji" alt=":wink:" loading="lazy" width="20" height="20"></p>
<aside class="quote no-group" data-username="sodapopcan" data-post="14" data-topic="45474">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/sodapopcan/48/34668_2.png" class="avatar"> sodapopcan:</div>
<blockquote>
<p>Moving on from <code>pluck</code>, from what you wrote I get the impression you haven’t grasped the full power of composability</p>
</blockquote>
</aside>
<p>I can’t be sure how far I am with grasping its full power. I like and use it whenever it makes sense to me. Just don’t really see the point and how it relates to:</p>
<aside class="quote no-group" data-username="sodapopcan" data-post="14" data-topic="45474">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/sodapopcan/48/34668_2.png" class="avatar"> sodapopcan:</div>
<blockquote>
<p>You certainly shouldn’t be needing to write SQL first to turn it into Ecto with any regularity</p>
</blockquote>
</aside>
<p>The very first example from this very thread</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">query = 
  from cr in Chatroom, 
    join: ucr in assoc(:users_chatrooms), 
    on: ucr.user_id == ^user_id and ucr.chatroom_id == cr.id,
    select: map(cr, [:id, :name])

Repo.all(query)
</code></pre>
<p>If that’s not writing SQL in a convoluted (for SQL savvy person) syntax?</p>
<p>And for something that I know can be as simple as <code>user.chatrooms.pluck(:id, :name)</code></p>
<p>The same for <code>user.chatrooms.count</code>, which is obvious what it does, and I both understand and remember it the moment I see it but had to google the other day how to express this to make Ecto happy. And guess what? The first thing I found was another writing another SQL query in Ecto syntax.</p>
<p>Yes, I know that Ecto is not an ORM, It is closer to Arel for example, on top of which an ORM could be built. Yes, I know there’s also “pipe syntax” which while still far away from the simplicity of the ORM syntax is much more palatable for me. If for one reason then for the fact that it does not resemble SQL so much as to start questioning its added value… even if that’s only for fooling myself <img src="https://forum.elixirforum.com/images/emoji/apple/wink.png?v=15" title=":wink:" class="emoji" alt=":wink:" loading="lazy" width="20" height="20"></p>
<p>In any case, my problem was swiftly solved by Benjamin and the rest is mostly the result of my personal “love-hate” relationship with Elixir and its libs.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="241966" 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/has-many-through-load-selected-associated-columns/45474/15">Post #14</a>
	                </div>
	            </div>
              <div id="likers-container-241966" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="241966"
                     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 #14"></div>
  </section>
</div>
    <div class="postbit" id="241972" data-post-id="241972">
  <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">
								<aside class="quote no-group" data-username="silverdr" data-post="15" data-topic="45474">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/silverdr/48/23771_2.png" class="avatar"> silverdr:</div>
<blockquote>
<p>Still I very, very rarely “grab all the fields/columns” when I don’t need them. That’s because I very rarely am fully convinced that the table I grab things from today is not going to expand beyond the realm of what I find “decent” for this approach if not tomorrow then a bit later.</p>
</blockquote>
</aside>
<p>Ecto only fetches the fields present in the schema anyways. You could even have multiple schemas for a single table, but different use cases. If you’re hardly fetching all fields of a schema I’d wonder if the schema is well defined or if you’d not rather want to split it into multiple.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="241972" data-batch-url="/posts/batch_likers">
                        4
                      </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/has-many-through-load-selected-associated-columns/45474/16">Post #15</a>
	                </div>
	            </div>
              <div id="likers-container-241972" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="241972"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-most-liked cat-most-liked" title="One of the top 3 liked posts in this thread!"></div>
  </section>
</div>
    <div class="postbit" id="242007" data-post-id="242007">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="silverdr" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/silverdr/120/23771_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  silverdr
                    <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">
								<aside class="quote no-group" data-username="LostKobrakai" data-post="16" data-topic="45474">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/lostkobrakai/48/3072_2.png" class="avatar"> LostKobrakai:</div>
<blockquote>
<p>You could even have multiple schemas for a single table, but different use cases</p>
</blockquote>
</aside>
<p>Which would make the problem of a) querying unneeded baggage, b) fetching that, and c) plucking required fields from a large record go away indeed. Shall keep that in mind as a good alternative in appropriate cases!</p>
<aside class="quote no-group" data-username="LostKobrakai" data-post="16" data-topic="45474">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/lostkobrakai/48/3072_2.png" class="avatar"> LostKobrakai:</div>
<blockquote>
<p>If you’re hardly fetching all fields of a schema</p>
</blockquote>
</aside>
<p>I didn’t say that. It is when I don’t need it, especially in a busy application spot, I don’t want to do all the three things I mentioned above.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="242007" 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/has-many-through-load-selected-associated-columns/45474/17">Post #16</a>
	                </div>
	            </div>
              <div id="likers-container-242007" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="242007"
                     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>