Q:

Rails Active Record Polymorphic Nested Resource Navigation

I've run into a bit of an issue and not sure how to get round it.
We have a number of polymorphic nested resources in our datamodel, eg:
Destination > Accommodation > Address
Destination > Attraction > Address
So it is possible to arrive at the Address controller from multiple parents. I need to be able to associate these correctly and also navigate back up the tree of parents.
Address is the same model in these cases, so my first solution for
this was to created nested resources in the routes file.
We then also started to use this nesting to provide a breadcrumb
navigation thing, so when our URLS get like this:
localhost:3000/destinations/1/accommodations/3/address/new
We can split it up and use it to navigate back down the path to any level.
I also, to make the controller generic, I use the nested resources to
work out what the parent resource for map is, so the controller looks
like this:
def new
     @parent = find_parent_model

     if !@parent.nil?
     @destination = @parent.destinations.new

[...]
def find_parent
   params.each do |name, value|
     if name =~ /(.+)_id$/
       return $1.classify.constantize.find(value)
     end
   end
   nil
 end

This works. But the problem is that we have 1800 lines of nested resources in the routes.rb file and now it takes the rails app about 5 minutes to start, and it sits
there using 500MB of ram. :S
Does anyone know of a less crazy way of doing this?

A:

You might want to give up on using the nested resources syntax for the routing.
A single route like 
get 'destinations/:destination_id/:parent_type/:parent_id/address/new' => 'address#new'

would match all of resources, and in AddressController#new you could have
@parent = params[:parent_type].constantize.find(params[:parent_id])

You might also want to check that @parent is of one of the expected types afterwards.

