# (REST) Nested routes

**URL:** <https://www.ruby-forum.com/t/rest-nested-routes/127263>\
**Category:** Rails\
**Created:** [January 27, 2008, 10:09am UTC](https://www.ruby-forum.com/t/rest-nested-routes/127263 "2008-01-27T10:09:22Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![eyp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e274bd/32.png) [@eyp](https://www.ruby-forum.com/u/eyp)\
**Post date:** [January 27, 2008, 10:09am UTC](https://www.ruby-forum.com/t/rest-nested-routes/127263/1 "2008-01-27T10:09:22Z")

</div>

Hello, I’m in troubles with nested routes. I have three models: Team,  
Tournament and TeamTournaments

class Team \< ActiveRecord::Base  
has\_many :tournaments, :through =\> :team\_tournaments  
has\_many :team\_tournaments

class TeamTournament \< ActiveRecord::Base  
belongs\_to :team, :foreign\_key =\> ‘team\_id’  
belongs\_to :tournament, :class\_name =\> ‘League’, :foreign\_key =\>  
‘tournament\_id’

class Tournament \< ActiveRecord::Base  
has\_many :teams, :through =\> :team\_tournaments  
has\_many :team\_tournaments

So it’s a N-M relationship between Team and Tournament through  
TeamTournaments.

Well, I want to have nested routes from Team to Tournament:

teams/1/tournaments/1

so I’ve mapped it in routes.rb:

I’ve tried these two forms:  
map.resources :teams, :has\_many =\> [:tournaments]  
map.resources :teams do |team|  
team.resources :tournaments  
end

And I can do [http://localhost:3000/teams/1/tournaments/1](http://localhost:3000/teams/1/tournaments/1), but I can’t  
use tournament\_url(:team\_id =\> @team, :id =\> @team.leagues[0]) in a  
view because I get the error tournament\_url is not defined.

What’s wrong?

---

<div class="post-metadata">

**Author:** ![eyp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e274bd/32.png) [@eyp](https://www.ruby-forum.com/u/eyp)\
**Post date:** [January 27, 2008, 10:30am UTC](https://www.ruby-forum.com/t/rest-nested-routes/127263/2 "2008-01-27T10:30:29Z")

</div>

If I’m not mistaken,

> map.resources :teams, :has\_many =\> [:tournaments]  
> map.resources :teams do |team|  
> team.resources :tournaments  
> end

does exactly the same thing… so I think you only need the first line  
(at least in Rails 2.0).

The fact that tournaments\_url doesn’t get recognized is because the  
only way you have it routed is when it’s scoped by a team.

You would need to scope it with a team in order to get a proper url,  
(e.g. team\_tournament\_url(@team, @tournament). You may be looking to  
get routes for both directions so then you need to define your routes  
as such:

map.resources :teams, :has\_many=\>[:tournaments]  
map.resources : tournaments, :has\_many=\>[:teams]

It’s late and I’m not 100% thinking through your situation, so take it  
with a grain of salt.

-Jason

---

<div class="post-metadata">

**Author:** ![eyp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e274bd/32.png) [@eyp](https://www.ruby-forum.com/u/eyp)\
**Post date:** [January 27, 2008, 10:38am UTC](https://www.ruby-forum.com/t/rest-nested-routes/127263/3 "2008-01-27T10:38:06Z")

</div>

> You would need to scope it with a team in order to get a proper url,  
> (e.g. team\_tournament\_url(@team, @tournament).

Yes, that’s right, I forgot to prefix tournament\_url with ‘team\_’. As  
always, Rails is more easier than anybody could think.

Thanks Jason.

---

<div class="post-metadata">

**Author:** ![eyp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e274bd/32.png) [@eyp](https://www.ruby-forum.com/u/eyp)\
**Post date:** [February 3, 2008, 5:25am UTC](https://www.ruby-forum.com/t/rest-nested-routes/127263/4 "2008-02-03T05:25:05Z")

</div>

hi there, i have a problem…  
i have 3 tables as below  
class1 - id, classname  
student - id, class1\_id  
timetable - class1\_id

im having a problme to convert sql to ruby-readable sql.  
can u assist me?  
i want to have something like…  
the output will show the timetable of the student… by define the  
class first.  
please help me… thanks

On Jan 27, 5:36 pm, “Eduardo Yáñez Parareda” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![eyp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e274bd/32.png) [@eyp](https://www.ruby-forum.com/u/eyp)\
**Post date:** [February 3, 2008, 5:20pm UTC](https://www.ruby-forum.com/t/rest-nested-routes/127263/5 "2008-02-03T17:20:32Z")

</div>

Eduardo YÃ¡Ã±ez Parareda wrote:

> Hello, I’m in troubles with nested routes. I have three models: Team,  
> Tournament and TeamTournaments

I see Jason found the problem for you. If you get confused over routes  
again, just use the “rake routes” command to see all your routes and  
their names. It’s the best way to know what’s going on when you do  
map.resources.

> class Team \< ActiveRecord::Base  
> has\_many :tournaments, :through =\> :team\_tournaments  
> has\_many :team\_tournaments
> 
> class TeamTournament \< ActiveRecord::Base  
> belongs\_to :team, :foreign\_key =\> ‘team\_id’  
> belongs\_to :tournament, :class\_name =\> ‘League’, :foreign\_key =\>  
> ‘tournament\_id’
> 
> class Tournament \< ActiveRecord::Base  
> has\_many :teams, :through =\> :team\_tournaments  
> has\_many :team\_tournaments

By the way, those models look like they could be cleaned up a little.

class Team \< ActiveRecord::Base  
has\_many :team\_tournaments, :dependent =\> :destroy  
has\_many :tournaments, :through =\> :team\_tournaments

class TeamTournament \< ActiveRecord::Base  
belongs\_to :team  
belongs\_to :tournament, :class\_name =\> ‘League’

class Tournament \< ActiveRecord::Base  
has\_many :team\_tournaments, :dependent =\> :destroy  
has\_many :teams, :through =\> :team\_tournaments

I added the :dependent option, which cleans up the join model records  
when they are no longer useful because one of the records being joined  
no longer exists. The other thing I changed was removing the  
:foreign\_key option on the belongs\_to associations. The :team  
association would never need to explicitly specify that, and in Rails 2  
neither would the :tournament. I’m a little confused what’s going on  
with setting the :class\_name to League, but I assume you have some STI  
going on that I can’t see.

–  
Josh S.  
[http://blog.hasmanythrough.com](http://blog.hasmanythrough.com)
