title: Best practices to handle routes for STI subclasses in rails
body: <p>My Rails views and controllers are littered with <code>redirect_to</code>, <code>link_to</code>, and <code>form_for</code> method calls. Sometimes <code>link_to</code> and <code>redirect_to</code> are explicit in the paths they're linking (e.g. <code>link_to 'New Person', new_person_path</code>), but many times the paths are implicit (e.g. <code>link_to 'Show', person</code>). </p>
  
  <p>I add some single table inheritance (STI) to my model (say <code>Employee &lt; Person</code>), and all of these methods break for an instance of the subclass (say <code>Employee</code>); when rails executes <code>link_to @person</code>, it errors with <code>undefined method employee_path' for #&lt;#&lt;Class:0x000001022bcd40&gt;:0x0000010226d038&gt;</code>. Rails is looking for a route defined by the class name of the object, which is employee. These employee routes are not defined, and there is no employee controller so the actions aren't defined either.</p>
  
  <p>This question has been asked before:</p>
  
  <ol>
  <li>At <a href="https://stackoverflow.com/questions/1773367/link-to-issue-with-inherited-active-record-class">StackOverflow</a>, the answer is to edit every instance of link_to etc in your entire codebase, and state the path explicitly</li>
  <li>On <a href="https://stackoverflow.com/questions/702728/get-route-for-base-class-of-sti-class-in-rails">StackOverflow</a> again, two people suggest using <code>routes.rb</code> to map the subclass resources to the parent class (<code>map.resources :employees, :controller =&gt; 'people'</code>). The top answer in that same SO question suggests type-casting every instance object in the codebase using <code>.becomes</code></li>
  <li>Yet another one at <a href="https://stackoverflow.com/questions/4432858/broken-rails-routes-after-implementing-single-table-inheritance">StackOverflow</a>, the top answer is way in the Do Repeat Yourself camp, and suggests creating duplicate scaffolding for every subclass.</li>
  <li><a href="https://stackoverflow.com/questions/943300/single-table-inheritance-routing">Here's</a> the same question again at SO, where the top answer seems to just be wrong (Rails magic Just Works!)</li>
  <li>Elsewhere on the web, I found <a href="http://strugglingwithruby.blogspot.com/2008/10/single-table-inheritance.html" rel="noreferrer">this blog post</a> where F2Andy recommends editing in the path everywhere in the code.</li>
  <li>On the blog post <a href="http://blog.lrdesign.com/2009/03/single-table-inheritance-and-restful-routes/" rel="noreferrer">Single Table Inheritance and RESTful Routes</a> at Logical Reality Design, it is recommended to map the resources for the subclass to the superclass controller, as in SO answer number 2 above.</li>
  <li>Alex Reisner has a post <a href="http://www.alexreisner.com/code/single-table-inheritance-in-rails" rel="noreferrer">Single Table Inheritance in Rails</a>, in which he advocates against mapping the resources of the child classes to the parent class in <code>routes.rb</code>, since that only catches routing breakages from <code>link_to</code> and <code>redirect_to</code>, but not from <code>form_for</code>. So he recommends instead adding a method to the parent class to get the subclasses to lie about their class. Sounds good, but his method gave me the error <code>undefined local variable or method `child' for #</code>.</li>
  </ol>
  
  <p>So the answer that seems most elegant and has the most consensus (but it's not all <em>that</em> elegant, nor <em>that</em> much consensus), is the add the resources to your <code>routes.rb</code>. Except this doesn't work for <code>form_for</code>. I need some clarity! To distill the choices above, my options are</p>
  
  <ol>
  <li>map the resources of the subclass to the controller of the superclass in <code>routes.rb</code> (and hope I don't need to call form_for on any subclasses)</li>
  <li>Override rails internal methods to make the classes lie to each other</li>
  <li>Edit every instance in the code where the path to an object's action is invoked implicitly or explicitly, either changing the path or type-casting the object.</li>
  </ol>
  
  <p>With all these conflicting answers, I need a ruling. It seems to me like there is no good answer. Is this a failing in rails' design? If so, is it a bug that may get fixed? Or if not, then I'm hoping someone can set me straight on this, walk me through the pros and cons of each option (or explain why that's not an option), and which one is the right answer, and why. Or is there a right answer that I'm not finding on the web?</p>