# Option for categories

**URL:** <https://www.ruby-forum.com/t/option-for-categories/141887>\
**Category:** Rails\
**Created:** [July 1, 2008, 1:11pm UTC](https://www.ruby-forum.com/t/option-for-categories/141887 "2008-07-01T13:11:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![torso](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/34f0e0/32.png) [@torso](https://www.ruby-forum.com/u/torso)\
**Post date:** [July 1, 2008, 1:11pm UTC](https://www.ruby-forum.com/t/option-for-categories/141887/1 "2008-07-01T13:11:42Z")

</div>

Hi

I need to do options for categories in view

in controller i have action

def new\_to\_do  
@categories = Category.find(:all)  
end

new\_to\_do template contains

**Category:** \<%=options\_from\_collection\_for\_select @categories, "id", "category", @item.category\_id %\>

I get error : You have a nil object when you didn’t expect it!  
The error occurred while evaluating nil.category\_id

So i have 3 tables categories, items, notes  
Items belongs to category and note, so I have add those to the model  
of item. Do i have to add category model has\_many Items call also?

I appreciate that if sombody could help.

---

<div class="post-metadata">

**Author:** ![torso](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/34f0e0/32.png) [@torso](https://www.ruby-forum.com/u/torso)\
**Post date:** [July 1, 2008, 1:22pm UTC](https://www.ruby-forum.com/t/option-for-categories/141887/2 "2008-07-01T13:22:52Z")

</div>

> I get error : You have a nil object when you didn’t expect it!  
> The error occurred while evaluating nil.category\_id

You use @item.category\_id here without assigning anything to @item, so  
it’s nil

> So i have 3 tables categories, items, notes  
> Items belongs to category and note, so I have add those to the model  
> of item. Do i have to add category model has\_many Items call also?

Yes, Category needs a  
has\_many :items

if Items defined  
belongs\_to :category

Shouldn’t note belong\_to Item instead the other way around?  
I just assume this from the name, you may know better.

---

<div class="post-metadata">

**Author:** ![torso](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/34f0e0/32.png) [@torso](https://www.ruby-forum.com/u/torso)\
**Post date:** [July 1, 2008, 5:24pm UTC](https://www.ruby-forum.com/t/option-for-categories/141887/3 "2008-07-01T17:24:22Z")

</div>

Yeah. Note belongs to item. Thanks for replay, i have to tried it  
tomorrow.
