# Error: nested-routes with nested-model having foreign keys

**URL:** <https://www.ruby-forum.com/t/error-nested-routes-with-nested-model-having-foreign-keys/177789>\
**Category:** Rails\
**Created:** [October 23, 2009, 7:23pm UTC](https://www.ruby-forum.com/t/error-nested-routes-with-nested-model-having-foreign-keys/177789 "2009-10-23T19:23:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![nin\_d](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/4bbf92/32.png) [@nin\_d](https://www.ruby-forum.com/u/nin_d)\
**Post date:** [October 23, 2009, 7:23pm UTC](https://www.ruby-forum.com/t/error-nested-routes-with-nested-model-having-foreign-keys/177789/1 "2009-10-23T19:23:19Z")

</div>

Hi,

I have this Test, User and Project models where every test belongs\_to  
a project and project has\_many tests.  
Test also belongs\_to User as ‘owner’.

Then i followed railcast#139 to create nested route as -  
map.resources :projects, :has\_many =\> :tests

But somehow i get this error when url is ‘[http://127.0.0.1:3000/](http://127.0.0.1:3000/)  
projects/1/tests’ in tests/index.html.erb. Please help.

How line 24 works and not 25 :?

- Ninad

\*\*\*\* ERROR \*\*\*\*

NoMethodError in Tests#index

Showing app/views/tests/index.html.erb where line #25 raised:

You have a nil object when you didn’t expect it!  
The error occurred while evaluating nil.username

\*\*\*\* tests\_controller.rb \*\*\*

```
@project = Project.find(params[:project_id])
@tests = @project.tests.find(:all, :include =>

```

[:owner, :project, :category])

\*\*\*\* index.html.erb \*\*\*\*

20: \<% @tests.each do |testcase| %\>  
21:   
22: \<%=h test.status %\>  
23: \<%=h test.defunct %\>  
24: \<%=h test.project.title %\>  
25: \<%=h test.owner.username %\>  
26:   
27: \<% end %\>

---

<div class="post-metadata">

**Author:** ![nin\_d](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/4bbf92/32.png) [@nin\_d](https://www.ruby-forum.com/u/nin_d)\
**Post date:** [October 23, 2009, 9:46pm UTC](https://www.ruby-forum.com/t/error-nested-routes-with-nested-model-having-foreign-keys/177789/2 "2009-10-23T21:46:10Z")

</div>

I observed that eager loading works when there is only one entry in  
tests model.  
Strange!

Any suggestions?

---

<div class="post-metadata">

**Author:** ![nin\_d](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/4bbf92/32.png) [@nin\_d](https://www.ruby-forum.com/u/nin_d)\
**Post date:** [October 24, 2009, 6:40am UTC](https://www.ruby-forum.com/t/error-nested-routes-with-nested-model-having-foreign-keys/177789/3 "2009-10-24T06:40:16Z")

</div>

Hi

> 20: \<% @tests.each do |testcase| %\>  
> 21:
> 
>   
> 22: \<%=h test.status %\>  
> 23: \<%=h test.defunct %\>  
> 24: \<%=h test.project.title %\>  
> 25: \<%=h test.owner.username %\>
> 
> ```
> Is it actually |testcase| or |test| ?
> 
> ```
> 
> Sijo

---

<div class="post-metadata">

**Author:** ![nin\_d](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/4bbf92/32.png) [@nin\_d](https://www.ruby-forum.com/u/nin_d)\
**Post date:** [October 24, 2009, 6:51am UTC](https://www.ruby-forum.com/t/error-nested-routes-with-nested-model-having-foreign-keys/177789/4 "2009-10-24T06:51:03Z")

</div>

it is test not testcase!

---

<div class="post-metadata">

**Author:** ![nin\_d](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/4bbf92/32.png) [@nin\_d](https://www.ruby-forum.com/u/nin_d)\
**Post date:** [October 24, 2009, 2:37pm UTC](https://www.ruby-forum.com/t/error-nested-routes-with-nested-model-having-foreign-keys/177789/5 "2009-10-24T14:37:33Z")

</div>

I see some issue with eager loading. This post is little confusing. I  
will create another one.
