Finding a nested resource's 'parent' object?

Say I have this setup:

class Ticket < ActiveRecord::Base
belongs_to :project

map.resources :projects do |p|
p.resources :tickets

How would I go about programatically figuring out that Ticket is
nested under Project?

Future reference:

def get_parent_resource(target_klass, path)
  # Convert the requested path into hash form
  hash = ActionController::Routing::Routes.recognize_path

(path, :method => :get)

  # Loop through path keys and see if any end in '_id' and our kid

class belongs_to the associated AR Class
pair = hash.detect{|k, v| k.to_s.ends_with?("_id") &&

  # Load up the AR class based on the matching path pair
  klass = pair[0].to_s[0...-3].classify.constantize

  # Find and return the target parent object

I’m sure it doesn’t work in all cases, but I think it gets pretty

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs