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

**URL:** <https://www.ruby-forum.com/t/nested-routes-with-has-many-associations-via-a-foreign-key/191114>\
**Category:** Rails\
**Created:** [July 5, 2010, 8:54pm UTC](https://www.ruby-forum.com/t/nested-routes-with-has-many-associations-via-a-foreign-key/191114 "2010-07-05T20:54:14Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/58956e/32.png) [@dee](https://www.ruby-forum.com/u/dee)\
**Post date:** [July 5, 2010, 8:54pm UTC](https://www.ruby-forum.com/t/nested-routes-with-has-many-associations-via-a-foreign-key/191114/1 "2010-07-05T20:54:14Z")

</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  
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.

---

<div class="post-metadata">

**Author:** ![dee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/58956e/32.png) [@dee](https://www.ruby-forum.com/u/dee)\
**Post date:** [July 6, 2010, 12:55pm UTC](https://www.ruby-forum.com/t/nested-routes-with-has-many-associations-via-a-foreign-key/191114/2 "2010-07-06T12: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’,  
: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.
