Say that I have two resources, Project and Task. A Project can have many Tasks; A Task belongs to one Project. Also say that I have Task nested under Project in routes.rb:
map.resources :projects do |project| project.resources :tasks end
Can one programmatically discover this relationship? Basically, I need to dynamically load an arbitrary object, then figure out if it has a ‘parent’, and then load that parent.
Any ideas?
Routing will not help you as this is only meant to be used the other way around. What you can do is aliasing the relationship with :parent:
And then use this relationship to detect if a parent object is available:
Moreover, you can use polymorphic routes if the routes are set up correctly: