# Controller / Model confusion

**URL:** <https://www.ruby-forum.com/t/controller-model-confusion/52839>\
**Category:** Rails\
**Created:** [January 19, 2006, 5:47pm UTC](https://www.ruby-forum.com/t/controller-model-confusion/52839 "2006-01-19T17:47:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Matt\_Lee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9fc348/32.png) [@Matt\_Lee](https://www.ruby-forum.com/u/Matt_Lee)\
**Post date:** [January 19, 2006, 5:47pm UTC](https://www.ruby-forum.com/t/controller-model-confusion/52839/1 "2006-01-19T17:47:49Z")

</div>

I have created a product, with two controllers and two models, one for  
categories and one for articles. In my models directory, I have  
article.rb and articles.rb and seem to have code that relies on both of  
these!

I am unable to get any validation working because of this (whichever  
model I put the code in, it doesn’t load it)

Should I just rip it all apart and start again?

---

<div class="post-metadata">

**Author:** ![Matt\_Lee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9fc348/32.png) [@Matt\_Lee](https://www.ruby-forum.com/u/Matt_Lee)\
**Post date:** [January 19, 2006, 10:05pm UTC](https://www.ruby-forum.com/t/controller-model-confusion/52839/2 "2006-01-19T22:05:13Z")

</div>

the name of the controller file should be article.rb, and the class  
ArticleController

Write some tests, see what is depending on the other class, and  
refactor.

matt

---

<div class="post-metadata">

**Author:** ![Matt\_Lee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9fc348/32.png) [@Matt\_Lee](https://www.ruby-forum.com/u/Matt_Lee)\
**Post date:** [January 19, 2006, 10:08pm UTC](https://www.ruby-forum.com/t/controller-model-confusion/52839/3 "2006-01-19T22:08:14Z")

</div>

Matt,

not sure what you’ve done to get what you’ve got, but the destroy script  
will certainly help you clean up properly (i.e: it removes the tests and  
helpers and stuff)

e.g:

ruby script/destroy controller article

John.  
p.s: we off for a curry tomorrow night? Omar’s in Bradford yeah? 😉  
p.p.s: anyone in Bradford, UK is welcome to join us too 🙂

---

<div class="post-metadata">

**Author:** ![Matt\_Lee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9fc348/32.png) [@Matt\_Lee](https://www.ruby-forum.com/u/Matt_Lee)\
**Post date:** [January 19, 2006, 11:36pm UTC](https://www.ruby-forum.com/t/controller-model-confusion/52839/4 "2006-01-19T23:36:00Z")

</div>

Now getting:-

You have a nil object when you didn’t expect it!  
You might have expected an instance of Array.  
The error occured while evaluating nil.inject

The offending snippet of code is:-

 \<%= options\_from\_collection\_for\_select categories, "id", "category", article.category\_id %\>

My form itself is in a partial, which I am rendering with:-

\<%= render :partial =\> ‘form’, :locals =\> {:article =\> @article,  
:categories =\> @categories} %\>

matt

---

<div class="post-metadata">

**Author:** ![Matt\_Lee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9fc348/32.png) [@Matt\_Lee](https://www.ruby-forum.com/u/Matt_Lee)\
**Post date:** [January 20, 2006, 10:17am UTC](https://www.ruby-forum.com/t/controller-model-confusion/52839/5 "2006-01-20T10:17:35Z")

</div>

Can’t really see what’s going on there, but what you SHOULD have is:

you should have two controllers called categories.rb and articles.rb in  
the controllers folder.

And your models folder should contain the singular of these, so  
category.rb and article.rb.

Other than that, your partial seems correct. You sure @categories and  
@article are set correctly in your controller? They appear to be nil.  
Have a look at the Sql in your logfiles and run it on your database to  
make sure it returns the data you’re expecting.

Is the name of your field that holds your category name called category?

Hope any of this helps you find the problem.

Joerg
