Note that there are some explanatory texts on larger screens.

plurals
  1. POHow many classes is too many? Rails STI
    text
    copied!<p>I am working on a very large Rails application. We initially did not use much inheritance, but we have had some eye opening experiences from a consultant and are looking to refactor some of our models.</p> <p>We have the following pattern a lot in our application:</p> <pre><code>class Project &lt; ActiveRecord::Base has_many :graph_settings end class GraphType &lt; ActiveRecord::Base has_many :graph_settings #graph type specific settings (units, labels, etc) stored in DB and very infrequently updated. end class GraphSetting &lt; ActiveRecord::Base belongs_to :graph_type belongs_to :project # Project implementation of graph type specific settings (y_min, y_max) also stored in db. end </code></pre> <p>This also results in a ton of conditionals in views, helpers and in the GraphSetting model itself. None of this is good.</p> <p>A simple refactor where we get rid of GraphType in favor of using a structure more like this:</p> <pre><code>class Graph &lt; ActiveRecord::Base belongs_to :project # Generic methods and settings end class SpecificGraph &lt; Graph # Default methods and settings hard coded # Project implementation specific details stored in db. end </code></pre> <p>Now this makes perfect sense to me, eases testing, removes conditionals, and makes later internationalization easier. However we only have 15 to 30 graphs.</p> <p>We have a very similar model (to complicated to use as an example) with close to probably 100 different 'types', and could potentially double that. They would all have relationships and methods they inheritated, some would need to override more methods then others. It seems like the perfect use, but that many just seems like a lot. </p> <p>Is 200 STI classes to many? Is there another pattern we should look at?</p> <p>Thanks for any wisdom and I will answer any questions.</p>
 

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