Optimizing Rails routing definition

193 views Asked by At

We have about 650 routes defined in our Rails 3.2.22 app. Around 20 of them make up over 90% of our requests. These 20 all fall under an optional scope: (/:api_version). Where is the most optimal place to define these routes.

Is it at the beginning of the definition tree or the end? In other words, should they be defined by popularity in ascending or descending order?

2

There are 2 answers

0
chris raethke On BEST ANSWER

You should also consider splitting your routes into namespaces and also separate files.

There is a decent write up at http://blog.arkency.com/2015/02/how-to-split-routes-dot-rb-into-smaller-parts/

0
Bryan Dimas On

From the rails guides:

Rails routes are matched in the order they are specified, so if you have a resources :photos above a get 'photos/poll' the show action's route for the resources line will be matched before the get line.

So it would be most efficient to add the most used routes at the top.