Nested routes with has_many associations via a :foreign_key

My app calls for the use of :foreign_key into a model that it should
be nested with. I can't get the routes set up the way I want them.
Here is an example:

People buy and sell from each other. The purchase or sale is recorded
in an Order model.

class Person < ActiveRecord::Base
  has_many :purchases, :class_name => 'Order', :foreign_key =>
'buyer_id'
  has_many :sales, :class_name => 'Order', :foreign_key => 'seller_id'
...

class Order < ActiveRecord::Base
  belongs_to :seller, :class_name => "Person"
  belongs_to :buyer, :class_name => "Person"
...

The routes to display a list of purchases or sales is /people/1/orders?
buyer_id=1 and /people/1/orders?seller_id=1 respectively. The route
table has this entry:
  map.resources :people, :has_many => :orders

The Orders controller ferrets out which parameter was sent in and does
the appropriate search before rendering the index. (At this point, I
feel all sticky and want to take a shower.)

I'd really like to have nested routes such as /people/1/purchases and /
people/1/sales. So I'm kind of surprised I can't do this in Rails
2.3.8:
  map.resources :people do |people|
    people.resource :sales, :as => 'orders'
    people.resource :purchases, :as => 'orders'
  end

... or something similar to give me the correct nested routes.

Maybe you have a better way? Maybe I'm missing something.

Thanks,
d.

Dee wrote:

I'd really like to have nested routes such as /people/1/purchases and /
people/1/sales. So I'm kind of surprised I can't do this in Rails

You can do it with map.connect. You may have to play with the syntax,
but I think this will work:

map.connect 'people/:person_id/:controller',
            :action => 'index',
            :person_id => /\d+/

That will make 'people/1/sales' the equivalent of 'sales?person_id=1'

You'll then need to update you index action in your sales controller, to
detect params[:person_id] and use it to modify @sales. Or you can create
a custom action to handle this.