In Rails the default routes use the internal database id to identify the resource, so you end up with routes like:
/user/1/widget/4
It's possible to change these to use something other than :id
easily enough so that you could have routes like:
/user/bob/widget/favorites
But is there a way to have both available? I ask because in my case I'm using the route to create a unique id for use with an external service, but I'd like them to be based on a field other than id
because it's more useful to pass these alternative ids to the external service.
I can of course build something custom, but we currently have some code that works as follows (with other convenience functions on top; this is the core functionality) to get most of the functionality I would have to build 'for free' from Rails:
class PathIdParser
def initialize
@context = Application.routes
end
def parse(path)
@context.recognize_path(path)
end
def build(route, params)
@context.named_routes[route].format(params)
end
end
Obviously the build
function is easy enough to work with to use other routes by just changing the values passed into the params
hash, but is there a way I can get parse
to use these alternative fields to look up resources by, since recognize_path
seems to work based on the values returned by to_param
.