<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by josh c on Medium]]></title>
        <description><![CDATA[Stories by josh c on Medium]]></description>
        <link>https://medium.com/@jc_24?source=rss-99849b89dd01------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*HGFT13ahbY0cTNey</url>
            <title>Stories by josh c on Medium</title>
            <link>https://medium.com/@jc_24?source=rss-99849b89dd01------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 25 Nov 2026 00:58:41 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@jc_24/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Work notes: Do you need that ActiveRecord Model?]]></title>
            <link>https://tech.sh24.org.uk/work-notes-do-you-need-that-activerecord-model-3a75ac46ad03?source=rss-99849b89dd01------2</link>
            <guid isPermaLink="false">https://medium.com/p/3a75ac46ad03</guid>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[rails]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[ruby-on-rails]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[josh c]]></dc:creator>
            <pubDate>Thu, 20 Oct 2022 16:29:28 GMT</pubDate>
            <atom:updated>2022-10-20T16:29:28.529Z</atom:updated>
            <content:encoded><![CDATA[<p>In the project I’m working on we have an Address model. It has all the usual things you might expect:</p><ul><li>A few address_line fields</li><li>A country field</li><li>A postcode field</li><li>Some validations to ensure that the data stored by these fields is sensible</li></ul><pre>class Address &lt; ApplicationRecord</pre><pre>  belongs_to :addressable, polymorphic: true</pre><pre>  validates :address_line_1, :postcode, presence: true<br>  validates :address_line_1, length: { minimum: 5 }<br>  validates :address_line_1, :address_line_2, :address_line_3, :city, length: { maximum: 255 }<br>  validates :postcode, ...<br>  ...</pre><pre>end</pre><p>What more could you want?</p><p>Address relates to other models in our domain through a polymorphic association. To implement this we have a PhysicalAddressable concern:</p><pre>module PhysicalAddressable</pre><pre>  extend ActiveSupport::Concern</pre><pre>  included do<br>    has_one :address, as: :addressable, dependent: :destroy, inverse_of: :addressable</pre><pre>    delegate :address_line_1,  to: :address<br>    delegate :address_line_2,  to: :address<br>    delegate :address_line_3,  to: :address<br>    delegate :city,            to: :address<br>    delegate :postcode,        to: :address</pre><pre>    def self.search_by_postcode(postcode)<br>      ...<br>    end</pre><pre>    ...</pre><pre>  end</pre><pre>end</pre><p>Including this concern in a model allows it to have, and interact with, an address record.</p><p>Ok! All pretty standard Railsy stuff I hear you say 👍</p><p>This week I made some changes to PhysicalAddressable. These changes were compatible with all addressable models bar one: DistributionCentre</p><p>For the rest of this post, I’ll be talking through my thought process for resolving this problem and ultimately removing the address association from DistributionCentre .</p><h3>Context</h3><p>To help better model a shared structure which exists between various types of Order, we recently introduced a new model to our domain: EpisodeOfCare. Where we previously had a handful of addressable order models - these models will now each belong to an episode of care, the episode of care will handle association to the address.</p><figure><img alt="Entity diagram showing relationship between Address, Episode of Care, and Order" src="https://cdn-images-1.medium.com/max/541/1*mrTl1CM4vQE5Zu50zpDhTg.jpeg" /></figure><p>What I eventually need to do is disconnect Address from each of the addressable models, and instead associate Address to EpisodeOfCare.</p><p>One step towards this goal requires changing PhysicalAddressable so that each of the addressable models will pass calls for #address on to the associated episode of care.</p><pre>delegate :address, to: :episode_of_care</pre><p>This worked as planned for all but one of the addressable models…</p><h3>The trouble with DistributionCentre</h3><p>Unlike the other addressable models, DistributionCentre does not belong to an episode of care. So what should I do?</p><p>Options:</p><ol><li>Provide a means for directly associating DistributionCentre to Address (bypassing PhysicalAddressable)</li><li>Remove the association between DistributionCentre and Address, instead keeping the address values directly on the distribution centre</li></ol><p>The second option seems simpler; let’s see how viable it is.</p><h3>Does DistributionCentre need its own Address record?</h3><p>In the context of DistributionCentre, do we need:</p><ol><li>A (big A) Address — a fully fledged address object.</li><li>A (little a) address — an address value.</li></ol><p>To answer this question I want to know the following:</p><ul><li>How much logic do we have that is conditional on the address of a distribution centre?</li><li>How often do we make database queries for DistributionCentre which are conditional on the address?</li></ul><p><em>\Muffled sounds of grepping\</em></p><p>Well, it turns out that the answer to both questions is none/never/we don’t.</p><p>In fact:</p><ul><li>Most distribution centres don’t even have an address</li><li>Distribution centre addresses are not user interactable (no CRUDing!).</li></ul><p>So we’re not really getting much benefit from using an address record association for DistributionCentre.</p><ul><li>The validations are not particularly useful, since we’re only ever creating distribution centre addresses in a seed task, and we never update them.</li><li>We never query — or at an application level even care about — the contents of these records. As such, storing them in a separate table isn’t adding much value either.</li></ul><p>In the context of distribution centres: an address is an optional and static piece of information. It will be occasionally read, never queried, and never modified.</p><p>Knowing this, and since the existing association is now getting in the way of a necessary refactor, we have quite a convincing argument for removing the association between Address and DistributionCentre.</p><h3>Disconnecting Address and DistributionCentre</h3><p>Here’s a brief explanation of how I did this:</p><h3>Step 1.</h3><p>I need to add some address fields to the distribution centre table. Rails provides a handy migration generator which can do this in one line.</p><pre>rails generate migration AddAddressFieldsToDistributionCentres address_line_1:string address_line_2:string address_line_3:string city:string postcode:string country:string</pre><h3>Step 2.</h3><p>Next up, I need to populate the new distribution centre address fields by copying data from the old address records. This was done with a post-deploy task:</p><pre>Address.where(addressable_type: &#39;DistributionCentre&#39;).find_each do |address|<br>  distribution_centre = address.addressable<br>  address_values = address.slice(*ADDRESS_FIELDS)<br>  distribution_centre.update!(address_values)<br>end</pre><pre>ADDRESS_FIELDS = %i[<br>  address_line_1<br>  address_line_2<br>  address_line_3<br>  city<br>  postcode<br>  country<br>].freeze</pre><p>I also updated our seeds file to reflect the new database schema.</p><h3>Step 3.</h3><p>All distribution centre records now have the address data they need. I then checked that the public interface of DistributionCentre would not break after removing the address association (I ran the tests). This highlighted that we are in need of a method for providing a comma-separated address string.</p><pre>class DistributionCentre &lt; ApplicationRecord</pre><pre>  ...</pre><pre>  def address_string<br>    [<br>      address_line_1,<br>      address_line_2,<br>      address_line_3,<br>      city,<br>      postcode,<br>      country<br>    ].compact.reject(&amp;:empty?).join(&#39;, &#39;)<br>  end</pre><pre>  ...</pre><pre>end</pre><h3>Step 4.</h3><p>Finally, I got rid of the association by removing the PhysicalAddressable concern from DistributionCentre. Old records were removed with another post-deploy task:</p><pre>Address.where(addressable_type: &#39;DistributionCentre&#39;).destroy_all</pre><p>Job done.</p><h3>Closing thoughts</h3><p>It occurs to me that I could have replaced the address with just a single string field on distribution centre. Keeping the fields separate does make it easier to query against them if that did ever become necessary… but that isn’t something we currently need to do. Ah well, it’s not really of any significance at this point.</p><p>Finally, I find it interesting that most distribution centres don’t have an address — this could indicate that there’s scope for improvement in our domain modelling of this area. This is not currently causing any obvious problems, but it could be something to consider in the future.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3a75ac46ad03" width="1" height="1" alt=""><hr><p><a href="https://tech.sh24.org.uk/work-notes-do-you-need-that-activerecord-model-3a75ac46ad03">Work notes: Do you need that ActiveRecord Model?</a> was originally published in <a href="https://tech.sh24.org.uk/">SH:24 Product &amp; Engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>