# REST nested routes with has\_one

**URL:** <https://www.ruby-forum.com/t/rest-nested-routes-with-has-one/130445>\
**Category:** Rails\
**Created:** [February 28, 2008, 9:46am UTC](https://www.ruby-forum.com/t/rest-nested-routes-with-has-one/130445 "2008-02-28T09:46:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![yannis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/c57346/32.png) [@yannis](https://www.ruby-forum.com/u/yannis)\
**Post date:** [February 28, 2008, 9:46am UTC](https://www.ruby-forum.com/t/rest-nested-routes-with-has-one/130445/1 "2008-02-28T09:46:19Z")

</div>

Hi everyone,

I have 2 models:

class User \< ActiveRecord::Base  
has\_one :laboratory, :foreign\_key =\> “pi\_id”  
end

class Laboratory \< ActiveRecord::Base  
belongs\_to :pi, :class\_name =\> “User”, :foreign\_key =\> “pi\_id”  
end

I set the following routes (in routes.rb):

map.resources :laboratories  
map.resources :users, :has\_one =\> :laboratory

All the routes are generated as expected, as demonstrated by rake  
routes. In particular:

user\_laboratory GET /users/:user\_id/laboratory  
{:controller=\>“laboratories”, :action=\>“show”}

But if I try users/17/laboratory, I get a ‘Couldn’t find Laboratory  
without an ID’ error. Someone could help me with this?  
In advance thank you very much.  
Yannis

---

<div class="post-metadata">

**Author:** ![yannis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/c57346/32.png) [@yannis](https://www.ruby-forum.com/u/yannis)\
**Post date:** [February 28, 2008, 10:05am UTC](https://www.ruby-forum.com/t/rest-nested-routes-with-has-one/130445/2 "2008-02-28T10:05:29Z")

</div>

Yannis J. wrote:

> But if I try users/17/laboratory, I get a ‘Couldn’t find Laboratory  
> without an ID’ error. Someone could help me with this?

In LaboratoriesController#show do you have:

@laboratory = Laboratory.find(param[:id])

or:

if param[:user\_id]  
@laboratory = User.find(param[:user\_id]).laboratory  
else  
@laboratory = Laboratory.find(param[:id])  
end

?

---

<div class="post-metadata">

**Author:** ![yannis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/c57346/32.png) [@yannis](https://www.ruby-forum.com/u/yannis)\
**Post date:** [February 28, 2008, 10:11am UTC](https://www.ruby-forum.com/t/rest-nested-routes-with-has-one/130445/3 "2008-02-28T10:11:04Z")

</div>

Mark B. wrote:

> In LaboratoriesController#show do you have:
> 
> @laboratory = Laboratory.find(param[:id])
> 
> or:
> 
> if param[:user\_id]  
> @laboratory = User.find(param[:user\_id]).laboratory  
> else  
> @laboratory = Laboratory.find(param[:id])  
> end
> 
> ?

Hi Mark,  
Of course I had only @laboratory = Laboratory.find(param[:id])  
I fixed it and it’s working perfectly now.  
Thank you very much!!!  
Yannis
