Note that there are some explanatory texts on larger screens.

plurals
  1. POAvoiding STI in Rails
    primarykey
    data
    text
    <pre><code>class User &lt; ActiveRecord::Base has_one :location, :dependent =&gt; :destroy, :as =&gt; :locatable has_one :ideal_location, :dependent =&gt; :destroy, :as =&gt; :locatable has_one :birthplace, :dependent =&gt; :destroy, :as =&gt; :locatable end class Location &lt; ActiveRecord::Base belongs_to :locatable, :polymorphic =&gt; true end class IdealLocation &lt; ActiveRecord::Base end class Birthplace &lt; ActiveRecord::Base end </code></pre> <p>I can't really see any reason to have subclasses in this situation. The behavior of the location objects are identical, the only point of them is to make the associations easy. I also would prefer to store the data as an int and not a string as it will allow the database indexes to be smaller.</p> <p>I envision something like the following, but I can't complete the thought:</p> <pre><code>class User &lt; ActiveRecord::Base LOCATION_TYPES = { :location =&gt; 1, :ideal_location =&gt; 2, :birthplace =&gt; 3 } has_one :location, :conditions =&gt; ["type = ?", LOCATION_TYPES[:location]], :dependent =&gt; :destroy, :as =&gt; :locatable has_one :ideal_location, :conditions =&gt; ["type = ?", LOCATION_TYPES[:ideal_location]], :dependent =&gt; :destroy, :as =&gt; :locatable has_one :birthplace, :conditions =&gt; ["type = ?", LOCATION_TYPES[:birthplace]], :dependent =&gt; :destroy, :as =&gt; :locatable end class Location &lt; ActiveRecord::Base belongs_to :locatable, :polymorphic =&gt; true end </code></pre> <p>With this code the following fails, basically making it useless:</p> <pre><code>user = User.first location = user.build_location location.city = "Cincinnati" location.state = "Ohio" location.save! location.type # =&gt; nil </code></pre> <p>This is obvious because there is no way to translate the :conditions options on the has_one declaration into the type equaling 1.</p> <p>I could embed the id in the view anywhere these fields appear, but this seems wrong too:</p> <pre><code>&lt;%= f.hidden_field :type, LOCATION_TYPES[:location] %&gt; </code></pre> <p>Is there any way to avoid the extra subclasses or make the LOCATION_TYPES approach work?</p> <p>In our particular case the application is very location aware and objects can have many different types of locations. Am I just being weird not wanting all those subclasses?</p> <p>Any suggestions you have are appreciated, tell me I'm crazy if you want, but would you want to see 10+ different location models floating around app/models?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload