<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="314012" data-post-id="314012">
  <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>Keep in mind, the struct’s original state is available in <code>changeset.data</code>.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="314012" 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/update-shorthand-for-access-behavor/60537/22">Post #21</a>
	                </div>
	            </div>
              <div id="likers-container-314012" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="314012"
                     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="314792" data-post-id="314792">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="7rans" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/7rans/120/25322_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  7rans
                    <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">
								<blockquote>
<p>Keep in mind, the struct’s original state is available in <code>changeset.data</code>.</p>
</blockquote>
<p>I’m aware. My thought is that it would be much more convenient if the “working” data was the structure itself and the rest of the changeset structure were in an special metadata field, akin to <code>__meta__</code>.  That way you just work with the structure in normal fashion, and the Changeset functions take care of “diffing” after the fact.</p>
<p>Btw, I ran into a snag with using the changeset rather than the structure for my calculate procedure – it’s a lot more complex than I recalled. It has to calculate for each record in an assoc_many relation and each one of those actually gets delegated to a different module based on a “shape” field. it looks like this:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">def perimeter(%Feature{} = feature) do
    case feature.shape do
      :custom   -&gt; Shape.Custom.perimeter(feature)
      :square   -&gt; Shape.Square.perimeter(feature)
      :round    -&gt; Shape.Round.perimeter(feature)
      :fourbar  -&gt; Shape.Fourbar.perimeter(feature)
      :triangle -&gt; Shape.Triangle.perimeter(feature)
      _ -&gt; 0.0  # should never happen
    end
  end
</code></pre>
<p>Having to redo all of these (and more) to handle the changeset instead of the actual struct gives me pause.</p>
<p>I’m thinking now, that since a Map and a Struct are essentially polymorphic, then perhaps I can write a routine that can extract the data from the changeset and convert it to a map. and I can pass that into my calculate function.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="314792" 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/update-shorthand-for-access-behavor/60537/23">Post #22</a>
	                </div>
	            </div>
              <div id="likers-container-314792" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="314792"
                     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="314949" data-post-id="314949">
  <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">
								<aside class="quote no-group" data-username="7rans" data-post="23" data-topic="60537">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/7rans/48/25322_2.png" class="avatar"> 7rans:</div>
<blockquote>
<p>I’m aware. My thought is that it would be much more convenient if the “working” data was the structure itself and the rest of the changeset structure were in an special metadata field, akin to <code>__meta__</code>.</p>
</blockquote>
</aside>
<p>Ecto is very purposefully designed this way.  Again, it’s hard to know without seeing more code what you need to do, but I promise you there is an elegant FP solution here.  I do understand your problem from a high-level but I’ve never actually run into this myself so it’s hard to offer any further advice without getting speculative and rambling, lol.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="314949" 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/update-shorthand-for-access-behavor/60537/24">Post #23</a>
	                </div>
	            </div>
              <div id="likers-container-314949" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="314949"
                     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="315785" data-post-id="315785">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="7rans" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/7rans/120/25322_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  7rans
                    <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 did find a <em>reasonably</em> elegant solution. Basically, I realized, where it actually matters, when accessing a struct vs a changeset, was just getting the data via the field names. So I added the following to my struct/schema modules.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">  def get(%__MODULE__{} = record, field) do
    record
    |&gt; Map.get(field)
  end
  def get(%Ecto.Changeset{} = changeset, field) do
    changeset
    |&gt; Ecto.Changeset.get_field(field)
  end
</code></pre>
<p>I can just use <code>get()</code> and it will work in either case. That works well for the occasional use case.</p>
<p>For my calculations though, there is so much data access going on at various call depths, <code>get()</code> isn’t that helpful, so I took a different approach, similar to the one’s suggested by you (<a class="mention" href="/u/sodapopcan" rel="nofollow">@sodapopcan</a>), <a class="mention" href="/u/cmo" rel="nofollow">@cmo</a> and <a class="mention" href="/u/d4no0" rel="nofollow">@D4no0</a>.</p>
<p>Elixir being a functional language I realized I can apply the changeset and get the record and work with it without effecting the original changeset. So in my calculations I just return the <em>changes</em> made and apply those to the changeset or the struct depending on which I started with…</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">def calculate(%__MODULE__{} = record) do
  changes = calculate_changes(record)
  struct(record, changes)
end
def calculate(%Ecto.Changeset{} = record) do
  if record.valid? do
    record_struct = Ecto.Changeset.apply_changes(record)
    changes = calculate_changes(record_struct)
    Ecto.Changeset.change(record, changes)
  else
    record  # changeset has errors
  end
end
</code></pre>
<p>And with a little extra work (to pass along the parent data too) I was able to add the <code>calculate</code> call to the struct’s <code>changeset</code> function as <a class="mention" href="/u/d4no0" rel="nofollow">@D4no0</a> suggested.</p>
<p>One last thing, I realized I made a mistake with this post. I originally thought structs <em>had</em> access behavior b/c of their Map underpinnings. So my original idea of Access behavior for Changeset isn’t at all useful. However, the conversation turned out to be very useful. Much Thanks.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="315785" 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/update-shorthand-for-access-behavor/60537/25">Post #24</a>
	                </div>
	            </div>
              <div id="likers-container-315785" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="315785"
                     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>