# Help with understanding RESTful routes

**URL:** <https://www.ruby-forum.com/t/help-with-understanding-restful-routes/88957>\
**Category:** Rails\
**Created:** [March 15, 2007, 10:11pm UTC](https://www.ruby-forum.com/t/help-with-understanding-restful-routes/88957 "2007-03-15T22:11:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![pixellover](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ee59a6/32.png) [@pixellover](https://www.ruby-forum.com/u/pixellover)\
**Post date:** [March 15, 2007, 10:11pm UTC](https://www.ruby-forum.com/t/help-with-understanding-restful-routes/88957/1 "2007-03-15T22:11:40Z")

</div>

Hi,

I’m planning a new application and after all I’ve read a RESTful rails  
app  
seems to be perfect for the job. But since a few days I’m struggling  
with  
routing… here’s my simple test. If would be great if someone could  
enlighten me…

Three models: Person \<----\> Employment \<----\> Company

class Person \< ActiveRecord::Base  
has\_many :employments  
has\_many :companies, :through =\> :employments  
end

class Employment \< ActiveRecord::Base  
belongs\_to :company  
belongs\_to :person  
end

class Company \< ActiveRecord::Base  
has\_many :employments  
has\_many :people, :through =\> :employments  
end

I’ve created the models and generated all three controllers with the  
scaffold\_resource generator.

1. I would like the following URLs to work correct:

[http://localhost:3000/companies](http://localhost:3000/companies)  
[http://localhost:3000/companies/2](http://localhost:3000/companies/2)  
[http://localhost:3000/people](http://localhost:3000/people)

This is quite simple. I just added two routes:  
map.resources :companies  
map.resources :people

1. Now I would like the following to work:  
[http://localhost:3000/companies/1/people](http://localhost:3000/companies/1/people)

This should show me a list with all people assigned (via Employment) to  
company with ID=1.

This route seem’s like a starting point:  
map.resources :companies do |company|  
company.resources :people  
end

Now I think I must enhance the CompaniesController. Changing my  
controller to  
something like this…

def index  
@companies = Person.find(params[:person\_id]).companies  
end

… seems to work, but of course [http://localhost:3000/companies](http://localhost:3000/companies) stopped  
working.

Do I really have to change my index method again to decide between  
“person\_id  
is set, look for Person.find(:person\_id).companies” and “no person\_id,  
so do  
a Company.find(:all)”?

What happens when my application is growing and there are many models  
connected with my Person-model? Isn’t there some of this  
rails-magic-glue  
that’s doing the dirty work?

I also thought that calling something like  
[http://localhost/companies/42/people/new](http://localhost/companies/42/people/new) would automagically create the  
association between the Company with ID 42 and the newly entered  
Person…?

Regards,  
Timo

---

<div class="post-metadata">

**Author:** ![pixellover](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ee59a6/32.png) [@pixellover](https://www.ruby-forum.com/u/pixellover)\
**Post date:** [March 15, 2007, 10:46pm UTC](https://www.ruby-forum.com/t/help-with-understanding-restful-routes/88957/2 "2007-03-15T22:46:44Z")

</div>

Hi,

looking at your models I’d say you need 2 more resources: employees  
and employers.

NOTE: I am _not_ saying you need 2 more models. I’m saying 2 more  
resources.

See below:

On 15-Mar-07, at 2:06 PM, Timo S. wrote:

> Three models: Person \<----\> Employment \<----\> Company
> 
> [http://localhost:3000/companies](http://localhost:3000/companies)  
> This should show me a list with all people assigned (via  
> Employment) to  
> company with ID=1.
> 
> This route seem’s like a starting point:  
> map.resources :companies do |company|  
> company.resources :people  
> end

map.resources :companies do |company|  
company.resources :employees  
end

map.resources :people do |person|  
person.resources :employers  
end

employees\_controller and employers\_controller can then deal with the  
logic of your Employment model according to their specific needs  
without resorting to torturous logic like “I’m the companies  
controller but if I have a person\_id them I’m displaying a specific  
person’s company”. And, of course, you can use shared partials to  
keep your views dry.

HTH,  
Trevor

---

<div class="post-metadata">

**Author:** ![pixellover](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ee59a6/32.png) [@pixellover](https://www.ruby-forum.com/u/pixellover)\
**Post date:** [March 15, 2007, 10:50pm UTC](https://www.ruby-forum.com/t/help-with-understanding-restful-routes/88957/3 "2007-03-15T22:50:45Z")

</div>

Timo,

Maybe this will get your mind focused toward the correct solution:

[http://localhost:3000/employments/new](http://localhost:3000/employments/new)

Take a look at the following set of slides. Page down until you see a  
bright orange slide labeled “Constraints are liberating.” Then read  
though the next few slides.  
[http://www.loudthinking.com/lt-files/worldofresources.pdf](http://www.loudthinking.com/lt-files/worldofresources.pdf)

On Mar 15, 5:06 pm, Timo S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![pixellover](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ee59a6/32.png) [@pixellover](https://www.ruby-forum.com/u/pixellover)\
**Post date:** [March 15, 2007, 11:14pm UTC](https://www.ruby-forum.com/t/help-with-understanding-restful-routes/88957/4 "2007-03-15T23:14:50Z")

</div>

Hi,

> looking at your models I’d say you need 2 more resources: employees  
> and employers.

Well, after reading the rest of your post, this seems damn logical.  
Thanks for  
the enlightment. 🙂

> without resorting to torturous logic like “I’m the companies  
> controller but if I have a person\_id them I’m displaying a specific  
> person’s company”. And, of course, you can use shared partials to  
> keep your views dry.

I think this is the way to go, although I hoped for a little bit more  
magic…

Regards,  
Timo
