# Nested routes question

**URL:** <https://www.ruby-forum.com/t/nested-routes-question/194441>\
**Category:** Rails\
**Created:** [September 6, 2010, 4:39pm UTC](https://www.ruby-forum.com/t/nested-routes-question/194441 "2010-09-06T16:39:22Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 6, 2010, 4:39pm UTC](https://www.ruby-forum.com/t/nested-routes-question/194441/1 "2010-09-06T16:39:22Z")

</div>

Trying to convert my app (and brain) to RESTful routes from the old  
style. What is the accepted approach to nesting a resource within 2  
other resources?

Example:

Models

Person has\_many :scores  
Contest has\_many :scores  
Score belongs\_to :person, :contest

Routes

resources :people do  
resources :scores  
end

resources :competitions do  
resources :scores  
end

I want to be able to list scores by person, or by competition. Either  
of the these two paths will request the :index action in the :scores  
controller:

GET /people/person\_id/scores  
GET /contests/contest\_id/scores

So what is the best way to structure scores/index to deliver the  
appropriate list?

I could test for the presence of params[person\_id] or  
params[contest\_id], then execute and render accordingly within  
the :index action. But that seems somewhat inelegant. Is there a  
better way? Should I have two separate actions within the :score  
controller, and modify my route mapping somehow to request the  
appropriate action?

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 6, 2010, 6:00pm UTC](https://www.ruby-forum.com/t/nested-routes-question/194441/2 "2010-09-06T18:00:55Z")

</div>

bump … no answers?

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 7, 2010, 12:08pm UTC](https://www.ruby-forum.com/t/nested-routes-question/194441/3 "2010-09-07T12:08:28Z")

</div>

> I could test for the presence of params[person\_id] or  
> params[contest\_id], then execute and render accordingly within  
> the :index action. But that seems somewhat inelegant.

How about writing something generic to get the parent? You  
can add this at the bottom of your ScoresController for example:

private

def get\_parent  
params.each do |name, value|  
return $1.classify.constantize.find(value) if name =~ /(.+)\_id$/  
end  
nil  
end

Have fun,  
Franz

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 7, 2010, 11:51pm UTC](https://www.ruby-forum.com/t/nested-routes-question/194441/4 "2010-09-07T23:51:39Z")

</div>

DONT DEEPLY NEST ROUTES DOOOOOOOOOONT!!!

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 7, 2010, 9:31pm UTC](https://www.ruby-forum.com/t/nested-routes-question/194441/5 "2010-09-07T21:31:57Z")

</div>

Ed wrote:

> Trying to convert my app (and brain) to RESTful routes from the old  
> style. What is the accepted approach to nesting a resource within 2  
> other resources?

Well, REST only has to do with routing and external interface. It tells  
you nothing about the implementation details of how to structure your  
controllers, which seems to be your real question here.

> Example:
> 
> Models
> 
> Person has\_many :scores  
> Contest has\_many :scores  
> Score belongs\_to :person, :contest
> 
> Routes
> 
> resources :people do  
> resources :scores  
> end
> 
> resources :competitions do  
> resources :scores  
> end
> 
> I want to be able to list scores by person, or by competition. Either  
> of the these two paths will request the :index action in the :scores  
> controller:
> 
> GET /people/person\_id/scores  
> GET /contests/contest\_id/scores
> 
> So what is the best way to structure scores/index to deliver the  
> appropriate list?
> 
> I could test for the presence of params[person\_id] or  
> params[contest\_id], then execute and render accordingly within  
> the :index action. But that seems somewhat inelegant.

Does it? If you want to reuse the same controller action, it seems  
entirely reasonable to test the data you’re getting passed.

> Is there a  
> better way? Should I have two separate actions within the :score  
> controller, and modify my route mapping somehow to request the  
> appropriate action?

You could do that, certainly. I think I’d use the former solution,  
though.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 8, 2010, 12:47am UTC](https://www.ruby-forum.com/t/nested-routes-question/194441/6 "2010-09-08T00:47:15Z")

</div>

What is the difference between persons and contests as far as scores  
are concerned? Looks to be a polymorphic relationship based on the  
context of your post. Person to Scores and Scores to Contest.

I agree with radhames, though in lower case. I’ve found that you need  
only be concerned with one level of nesting at a time.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 8, 2010, 12:28am UTC](https://www.ruby-forum.com/t/nested-routes-question/194441/7 "2010-09-08T00:28:31Z")

</div>

On Sep 6, 4:35 am, Ed [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Score belongs\_to :person, :contest

I think that you should use HABTM :through, the rest should be easy

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 8, 2010, 2:34pm UTC](https://www.ruby-forum.com/t/nested-routes-question/194441/8 "2010-09-08T14:34:50Z")

</div>

Bb Serviss wrote:

> What is the difference between persons and contests as far as scores  
> are concerned? Looks to be a polymorphic relationship based on the  
> context of your post. Person to Scores and Scores to Contest.

That’s not my interpretation. It looks like a score belongs to a person  
and a contest simultaneously.

> I agree with radhames, though in lower case. I’ve found that you need  
> only be concerned with one level of nesting at a time.

That’s all that the OP _was_ concerned with, I think.

Best,  
–Â  
Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

Sent from my iPhone
