# Nil object for nested resource

**URL:** <https://www.ruby-forum.com/t/nil-object-for-nested-resource/107243>\
**Category:** Rails\
**Created:** [June 19, 2007, 4:19pm UTC](https://www.ruby-forum.com/t/nil-object-for-nested-resource/107243 "2007-06-19T16:19:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![turnkeytraders](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ba8739/32.png) [@turnkeytraders](https://www.ruby-forum.com/u/turnkeytraders)\
**Post date:** [June 19, 2007, 4:19pm UTC](https://www.ruby-forum.com/t/nil-object-for-nested-resource/107243/1 "2007-06-19T16:19:18Z")

</div>

Hi all,

I have 2 models with a standard 1 to many relationship. I’m also using  
REST on the two models and have added the appropriate code to the  
routes.rb file.

scorecard.rb:  
class Scorecard \< ActiveRecord::Base  
has\_many :attributes  
end

attribute.rb:  
class Attribute \< ActiveRecord::Base  
belongs\_to :scorecard  
end

routes.rb:  
map.resources :scorecards do |scorecard|  
scorecard.resources :attributes  
end

My problem is that I want to show a single scorecard with all it’s  
attributes. I then want to display a “New attribute” link to add a new  
attribute to the current scorecard.

scorecards/show.rhtml:  
\<%= label\_for(“scorecard”) %\>  
\<%=h @scorecard.name %\>

\<%= label\_for(“template”) %\>  
\<%=h @scorecard.template %\>

**Attributes:**

\<% for attribute in @scorecard.attributes %\> \<% end %\>

| \<%=h attribute.name %\> | \<%=h attribute.weight %\> |

\<%= link\_to "New Attribute", new\_attribute\_path(@scorecard) %\>

However I get the message for line 17 (last line) when I try to view  
this page,

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

I have tried checking against various REST and Rails books and articles,  
and I thought I had used the correct syntax for creating a link to a new  
attribute from this scorecard.

Any help on this would be greatly appreciated.

Thanks,

Matthew

---

<div class="post-metadata">

**Author:** ![turnkeytraders](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ba8739/32.png) [@turnkeytraders](https://www.ruby-forum.com/u/turnkeytraders)\
**Post date:** [June 20, 2007, 9:26pm UTC](https://www.ruby-forum.com/t/nil-object-for-nested-resource/107243/2 "2007-06-20T21:26:36Z")

</div>

I think you may need to rename your Attribute model. ActiveRecord  
objects already have an instance variable named “attributes” which  
holds the attributes for the given model instance. The Scorecard’s  
has\_many :attributes collection and the pre-existing attributes hash  
are probably colliding.

-John

On Jun 19, 7:19 am, Matthew L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![turnkeytraders](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ba8739/32.png) [@turnkeytraders](https://www.ruby-forum.com/u/turnkeytraders)\
**Post date:** [June 21, 2007, 12:12pm UTC](https://www.ruby-forum.com/t/nil-object-for-nested-resource/107243/3 "2007-06-21T12:12:54Z")

</div>

Problem solved!

In the routes.rb file I had the following

map.resources :scorecards do |scorecard|  
scorecard.resources :perspectives  
end

map.resources :perspectives do |perspective|  
perspective.resources: scores  
end

when I should have had

map.resources :scorecards do |scorecard|  
scorecard.resources :perspectives do |perspective|  
perspective.resources :score  
end  
end

Thanks,

Matthew
