# Issue with eager loading in nested routes

**URL:** <https://www.ruby-forum.com/t/issue-with-eager-loading-in-nested-routes/177817>\
**Category:** Rails\
**Created:** [October 24, 2009, 3:00pm UTC](https://www.ruby-forum.com/t/issue-with-eager-loading-in-nested-routes/177817 "2009-10-24T15:00:23Z")\
**Posts on this page:** 9\
**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 24, 2009, 3:00pm UTC](https://www.ruby-forum.com/t/issue-with-eager-loading-in-nested-routes/177817/1 "2009-10-24T15:00:23Z")

</div>

Hi Everyone,

I have issue while doing eager loading when url is =\>  
[http://127.0.0.1:3001/projects/1/features](http://127.0.0.1:3001/projects/1/features)  
I also did \<%= debug @features %\> to dump @feature object but I did  
not see ‘owner’ being eager-loaded.

Can anyone find out why owner is not being ‘eager-loaded’?

Thanks in advance.

========= features\_controller.rb → index method

def index  
@project = Project.find(params[:project\_id])  
@features = @project.features.find(:all, :include =\>  
[:owner, :project])

```
respond_to do |format|
  format.html # index.html.erb
  format.xml { render :xml => @features }
end

```

end

========= Output of \<%= debug @features %\>

* * *

- !ruby/object:Feature  
attributes:  
project\_id: “1”  
updated\_at: 2009-10-24 11:07:19  
created\_at: 2009-10-24 11:07:19  
attributes\_cache: {}

- !ruby/object:Feature  
attributes:  
project\_id: “1”  
updated\_at: 2009-10-24 11:26:09  
details: for testing  
priority: normal  
id: “3”  
owner\_id: “1”  
brief: test feature updated  
created\_at: 2009-10-24 11:18:11  
attributes\_cache: {}

========= Error in browser

NoMethodError in Features#index  
Showing app/views/features/index.html.erb where line #19 raised:

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

Extracted source (around line #19):

16: \<%=h feature.details %\>  
17: \<%=h feature.priority %\>  
18: \<%=h feature.project.title %\>  
19: \<%=h feature.owner.username %\>  
20: \<%= link\_to ‘Show’, [@project, feature] %\>  
21: \<%= link\_to ‘Edit’, edit\_project\_feature\_path(@project,  
feature) %\>  
22: \<%= link\_to ‘Destroy’, [@project, feature], :confirm =\>  
‘Are you sure?’, :method =\> :delete %\>

========= Server Log

ActionView::TemplateError (You have a nil object when you didn’t  
expect it!  
The error occurred while evaluating nil.username) on line #19 of app/  
views/features/index.html.erb:  
16: \<%=h feature.details %\>  
17: \<%=h feature.priority %\>  
18: \<%=h feature.project.title %\>  
19: \<%=h feature.owner.username %\>  
20: \<%= link\_to ‘Show’, [@project, feature] %\>  
21: \<%= link\_to ‘Edit’, edit\_project\_feature\_path(@project,  
feature) %\>  
22: \<%= link\_to ‘Destroy’, [@project, feature], :confirm =\>  
‘Are you sure?’, :method =\> :delete %\>

```
app/views/features/index.html.erb:19
app/views/features/index.html.erb:13:in `each'
app/views/features/index.html.erb:13
app/controllers/features_controller.rb:8:in `index'
-e:2:in `load'
-e:2

```

Rendered rescues/\_trace (94.0ms)  
Rendered rescues/\_request\_and\_response (0.0ms)  
Rendering rescues/layout (internal\_server\_error)

---

<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, 3:41pm UTC](https://www.ruby-forum.com/t/issue-with-eager-loading-in-nested-routes/177817/2 "2009-10-24T15:41:21Z")

</div>

On Oct 24, 1:58 pm, nin\_d [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi Everyone,
> 
> I have issue while doing eager loading when url is =\>[http://127.0.0.1:3001/projects/1/features](http://127.0.0.1:3001/projects/1/features)  
> I also did \<%= debug @features %\> to dump @feature object but I did  
> not see ‘owner’ being eager-loaded.
> 
> Can anyone find out why owner is not being ‘eager-loaded’?

Does the owner exist at all ? remove the :include, if the exception  
persists then you know eager loading is not at fault.

Fred

---

<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, 3:48pm UTC](https://www.ruby-forum.com/t/issue-with-eager-loading-in-nested-routes/177817/3 "2009-10-24T15:48:23Z")

</div>

Fred, owner do exist.

class Feature \< ActiveRecord::Base  
belongs\_to :project  
belongs\_to :owner, :class\_name =\> “User”, :foreign\_key =\> “owner\_id”  
end

Now in features controller, I have

@features = @project.features.find(:all, :include =\> :owner)

and dump for @features in view is where i see only two features being  
loaded and not the corresponding owner (only 1 in this case, with id

1. 
  - 

* * *

- !ruby/object:Feature  
attributes:  
project\_id: “1”  
updated\_at: 2009-10-24 11:07:19  
details: Represent Single Source Mode opcode  
priority: nornal  
id: “2”  
owner\_id: “1”  
brief: Opcode - SSM  
created\_at: 2009-10-24 11:07:19  
attributes\_cache: {}

- !ruby/object:Feature  
attributes:  
project\_id: “1”  
updated\_at: 2009-10-24 11:26:09  
details: for testing  
priority: normal  
id: “3”  
owner\_id: “1”  
brief: test feature updated  
created\_at: 2009-10-24 11:18:11  
attributes\_cache: {}

---

<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, 4:03pm UTC](https://www.ruby-forum.com/t/issue-with-eager-loading-in-nested-routes/177817/4 "2009-10-24T16:03:36Z")

</div>

Oops I had removed that myself. Damn!

Consider this please -

Now in features controller, I have  
@features = @project.features.find(:all, :include =\> :owner)  
and dump for @features in view is where i see only two features being  
loaded and not the corresponding owner (only 1 in this case, with id

1. 
  - 

* * *

- !ruby/object:Feature  
attributes:  
project\_id: “1”  
updated\_at: 2009-10-24 11:07:19  
details: some crap  
priority: nornal  
id: “2”  
owner\_id: “1”  
brief: foo  
created\_at: 2009-10-24 11:07:19  
attributes\_cache: {}
- !ruby/object:Feature  
attributes:  
project\_id: “1”  
updated\_at: 2009-10-24 11:26:09  
details: for testing  
priority: normal  
id: “3”  
owner\_id: “1”  
brief: test feature updated  
created\_at: 2009-10-24 11:18:11  
attributes\_cache: {}

---

<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, 3:57pm UTC](https://www.ruby-forum.com/t/issue-with-eager-loading-in-nested-routes/177817/5 "2009-10-24T15:57:10Z")

</div>

Hi –

On Sat, 24 Oct 2009, nin\_d wrote:

> @features = @project.features.find(:all, :include =\> :owner)
> 
> and dump for @features in view is where i see only two features being  
> loaded and not the corresponding owner (only 1 in this case, with id
> 
> 1. 
> -

Look again in your original post. There’s a debug dump of a Feature  
that has no owner\_id.

David

–  
The Ruby training with D. Black, G. Brown, J.McAnally  
Compleat Jan 22-23, 2010, Tampa, FL  
Rubyist [http://www.thecompleatrubyist.com](http://www.thecompleatrubyist.com)

David A. Black/Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))

---

<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, 4:59pm UTC](https://www.ruby-forum.com/t/issue-with-eager-loading-in-nested-routes/177817/6 "2009-10-24T16:59:48Z")

</div>

On Oct 24, 3:14 pm, nin\_d [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> In server log, I do see query for fetching users with id=1. How this  
> is not appearing in the index view??

Is there a user with id 1? Get it working without :include, then worry  
about the eager loading.

Fred

---

<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, 4:16pm UTC](https://www.ruby-forum.com/t/issue-with-eager-loading-in-nested-routes/177817/7 "2009-10-24T16:16:29Z")

</div>

In server log, I do see query for fetching users with id=1. How this  
is not appearing in the index view??

========= Server Log

Processing FeaturesController#index (for 127.0.0.1 at 2009-10-24  
19:42:45) [GET]  
Parameters: {“project\_id”=\>“1”}  
[4;36;1mProject Columns (0.0ms) [0m [0;1mSHOW FIELDS FROM  
`projects` [0m  
[4;35;1mProject Load (0.0ms) [0m [0mSELECT \* FROM `projects`  
WHERE (`projects`.`id` = 1) [0m  
[4;36;1mFeature Load (0.0ms) [0m [0;1mSELECT \* FROM `features`  
WHERE (`features`.project\_id = 1) [0m  
[4;35;1mFeature Columns (16.0ms) [0m [0mSHOW FIELDS FROM  
`features` [0m  
[4;36;1mUser Columns (16.0ms) [0m [0;1mSHOW FIELDS FROM `users`  
[0m  
[4;35;1mUser Load (0.0ms) [0m [0mSELECT \* FROM `users` WHERE  
(`users`.`id` = 1) [0m  
Rendering template within layouts/features  
Rendering features/index  
[4;36;1mCACHE (0.0ms) [0m [0;1mSELECT \* FROM `projects` WHERE  
(`projects`.`id` = 1) [0m  
[4;35;1mCACHE (0.0ms) [0m [0mSELECT \* FROM `projects` WHERE  
(`projects`.`id` = 1) [0m  
Rendered common/\_main\_menu (16.0ms)  
Rendered common/\_project\_menu (0.0ms)  
Completed in 141ms (View: 47, DB: 32) | 200 OK [[http://127.0.0.1/](http://127.0.0.1/)  
projects/1/features]

---

<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:08pm UTC](https://www.ruby-forum.com/t/issue-with-eager-loading-in-nested-routes/177817/8 "2009-10-24T18:08:17Z")

</div>

Fred, thanks! there s no user with id 1. My bad!

While destroying a record, is there any easier way (a hook in  
activerecord) to get warning/error that record belongs to other  
records (if at all it does)?

---

<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:09pm UTC](https://www.ruby-forum.com/t/issue-with-eager-loading-in-nested-routes/177817/9 "2009-10-24T18:09:28Z")

</div>

On Oct 24, 4:26 pm, nin\_d [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Fred, thanks! there s no user with id 1. My bad!
> 
> While destroying a record, is there any easier way (a hook in  
> activerecord) to get warning/error that record belongs to other  
> records (if at all it does)?

You can set a dependent option on associations to control this sort of  
stuff. If you want a real guarantee that this can’t ever happen, use a  
foreign key constraint.

Fred
