# Relationship breaks Validation

**URL:** <https://www.ruby-forum.com/t/relationship-breaks-validation/89881>\
**Category:** Rails\
**Created:** [March 23, 2007, 2:37pm UTC](https://www.ruby-forum.com/t/relationship-breaks-validation/89881 "2007-03-23T14:37:03Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![oem](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/919ad9/32.png) [@oem](https://www.ruby-forum.com/u/oem)\
**Post date:** [March 23, 2007, 2:37pm UTC](https://www.ruby-forum.com/t/relationship-breaks-validation/89881/1 "2007-03-23T14:37:03Z")

</div>

Heya people, this is my first post here so if I forget some relevant  
info (like logs or something) please tell me. Ok to my problem:  
I have a fairly simple rails app so far, a “products”-table that belongs  
to categories ( a simple one-to-many realtionship). I wanted to be able  
to select a category when editing/creating a product entry so I included  
a drop down list into the form for selecting a category:  
\<%= collection\_select( :product, :category\_id, @categories, :id, :name )  
%\>

The controller part of this:  
@product = Product.new  
@categories = Category.find(:all, :order =\> “name”)

The model (basic validation):  
validates\_presence\_of :title, :price, :category\_id, :description

If I enter everything correctly it works fine.  
But if I leave any field blank (so whenever the validation kicks in and  
finds something bad) I get following error message:

* * *

NoMethodError in Admin/products#create

Showing app/views/admin/products/new.rhtml where line #6 raised:

You have a nil object when you didn’t expect it!  
You might have expected an instance of Array.  
The error occurred while evaluating nil.inject  
Extracted source (around line #6):

3: \<%= text\_field ‘product’, ‘title’ %\>

  
4:  
5: 

Category  
  
6: \<%= collection\_select( :product, :category\_id, @categories, :id,  
:name ) %\>  
7:  
.  
.  
.  
Request

## Parameters: {“commit”=\>“Create”, “product”=\>{“price”=\>"", “title”=\>"", “category\_id”=\>“2”, “description”=\>""}}

It doesn’t matter what field is wrong and I could even kick validation  
for category\_id - it still happens.  
The error doesn’t happen if I remove the validation but I would really  
hate to disable that.  
So, any help would be greatly appreciated.  
Bye
