I have a Project resource and 5 types subclassing it via Single Table Inheritance. When I'm using the new URL generation in Rails 2.0 (i.e. <%= link_to project.name, project %> instead of <%= link_to project.name, project_path(project) %> for a SpecialProject (class SpecialProject < Project), Rails looks for special_project_path - and can't find it, obviously.
I think there are two ways to approach this problem:
1) map.resources :projects should also create routes for subclassed resources.
2) Polymorphic routing should be modified to use only a direct subclass of ActiveRecord::Base by default.
I have been thinking about whether or not this is a bug - and I think it is. I've only used STI a couple of times but when I did, I never defined controllers for every subclass but rather used ProjectsController to manage all types. This rules out option 1) mentioned above because special_project_path would map to SpecialProjectsController.
Option 2) seems to fit - if people are like me, most of the time they will use only one controller for subclassed resources. And if they don't, they still can define routes for their resources and write special_project_path(project) when needed.
I did a quick fix in the ActionController's polymorphic_routes.rb and replaced line 59 (base_segment = "#{RecordIdentifier.send!("#{inflection}_class_name", records.pop)}_" with the following code:
record = records.pop.class
if record.superclass == ActiveRecord::Base
base_segment = "#{RecordIdentifier.send!("#{inflection}_class_name", record)}_"
else
base_segment = "#{RecordIdentifier.send!("#{inflection}_class_name", record.superclass)}_"
end
This is a quick and dirty fix because I needed it - I'm not a Ruby geek (yet), so probably someone else could do a better job fixing it permanently.