# Nested routes with has\_many associations via a :foreign\_key

**URL:** <https://discuss.rubyonrails.org/t/nested-routes-with-has-many-associations-via-a-foreign-key/52757>\
**Category:** rubyonrails-talk\
**Created:** [July 5, 2010, 6:52pm UTC](https://discuss.rubyonrails.org/t/nested-routes-with-has-many-associations-via-a-foreign-key/52757 "2010-07-05T18:52:54Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![DGerton](https://avatars.discourse-cdn.com/v4/letter/d/7cd45c/32.png) [@DGerton](https://discuss.rubyonrails.org/u/DGerton)\
**Post date:** [July 5, 2010, 6:52pm UTC](https://discuss.rubyonrails.org/t/nested-routes-with-has-many-associations-via-a-foreign-key/52757/1 "2010-07-05T18:52:54Z")

</div>

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 &nbsp;&nbsp;has\_many :purchases, :class\_name =\> 'Order', :foreign\_key =\> 'buyer\_id' &nbsp;&nbsp;has\_many :sales, :class\_name =\> 'Order', :foreign\_key =\> 'seller\_id' ...

class Order \< ActiveRecord::Base &nbsp;&nbsp;belongs\_to :seller, :class\_name =\> "Person" &nbsp;&nbsp;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: &nbsp;&nbsp;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: &nbsp;&nbsp;map.resources :people do |people| &nbsp;&nbsp;&nbsp;&nbsp;people.resource :sales, :as =\> 'orders' &nbsp;&nbsp;&nbsp;&nbsp;people.resource :purchases, :as =\> 'orders' &nbsp;&nbsp;end

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

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

Thanks, d.

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [July 6, 2010, 10:55am UTC](https://discuss.rubyonrails.org/t/nested-routes-with-has-many-associations-via-a-foreign-key/52757/2 "2010-07-06T10:55:10Z")

</div>

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', &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:action =\> 'index', &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;: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.
