# Collection\_select validation problem

**URL:** <https://www.ruby-forum.com/t/collection-select-validation-problem/123969>\
**Category:** Rails\
**Created:** [December 22, 2007, 9:53pm UTC](https://www.ruby-forum.com/t/collection-select-validation-problem/123969 "2007-12-22T21:53:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 22, 2007, 9:53pm UTC](https://www.ruby-forum.com/t/collection-select-validation-problem/123969/1 "2007-12-22T21:53:19Z")

</div>

I’m trying to assign a parent foreign key value using  
collection\_select from my child “new” form. The problem I’m having is  
if I do not make a selection, I get the following error instead of the  
Rails validates\_presence\_of error:

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.map

If I make a selection, everything works just fine. What do I need to  
do to get the Rails validationvalidates\_presence\_of error?

Here’s the code:

MODEL

class Client \< ActiveRecord::Base  
has\_many :programs  
validates\_presence\_of :eid, :name  
end

class Program \< ActiveRecord::Base  
belongs\_to :client  
validates\_presence\_of :client\_id  
validates\_associated :client  
end

CONTROLLERS

def new  
@program = Program.new  
@clients = Client.find(:all, :order=\>“name”)

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

```

end

def create  
@program = Program.new(params[:program])

```
respond_to do |format|
  if @program.save
    flash[:notice] = 'Program was successfully created.'
    format.html { redirect_to(@program) }
    format.xml { render :xml => @program, :status

```

=\> :created, :location =\> @program }  
else  
format.html { render :action =\> “new” }  
format.xml { render :xml =\> @program.errors, :status  
=\> :unprocessable\_entity }  
end  
end  
end

VIIEW

\<%=  
collection\_select(‘program’,  
‘client\_id’,  
@clients,  
:id,  
:name,  
{ :include\_blank =\> true }  
)  
%\>

Thx for the assistance!

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 23, 2007, 8:54pm UTC](https://www.ruby-forum.com/t/collection-select-validation-problem/123969/2 "2007-12-23T20:54:31Z")

</div>

Still stumped on this. If I replace the collection\_select with a  
text\_field, the validates\_presence\_of :client\_id  
fires when I attempt to save the Program without a client\_id. BUT  
when I use collection\_select, and do not make a selection before  
attempting to to save the Program, I get the the nil object error. Why  
doesn’t the validate\_presence\_of: client\_id catch this?? I can see in  
the request that the client\_id is an empty. Shouldn’t that cause  
validates\_precence\_of to fail?

Can anyone explain to me what I’m doing wrong here?

Thanks,  
Jeff

{“commit”=\>“Create”,  
“authenticity\_token”=\>“4bec56fa77ee992dc0e3e13d169bf310ee7351ff”,  
“program”=\>{“eid”=\>“”,  
“name”=\>“”,  
“end\_dt(1i)”=\>“2007”,  
“end\_dt(2i)”=\>“12”,  
“end\_dt(3i)”=\>“23”,  
“client\_id”=\>“”,  
“url”=\>“”,  
“start\_dt(1i)”=\>“2007”,  
“end\_dt(4i)”=\>“13”,  
“start\_dt(2i)”=\>“12”,  
“end\_dt(5i)”=\>“45”,  
“start\_dt(3i)”=\>“23”,  
“start\_dt(4i)”=\>“13”,  
“start\_dt(5i)”=\>“45”}}

On Dec 22, 2:52 pm, “removed\_email\_address@domain.invalid” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 23, 2007, 9:22pm UTC](https://www.ruby-forum.com/t/collection-select-validation-problem/123969/3 "2007-12-23T21:22:07Z")

</div>

On 22 Dec 2007, at 20:52, removed\_email\_address@domain.invalid wrote:

> If I make a selection, everything works just fine. What do I need to  
> do to get the Rails validationvalidates\_presence\_of error?

Well if you encounter a validation error and render the ‘new’  
template. That template requires a @clients instance variable that you  
are setting up in the new action but not in the create action.

Fred

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 23, 2007, 11:39pm UTC](https://www.ruby-forum.com/t/collection-select-validation-problem/123969/4 "2007-12-23T23:39:47Z")

</div>

Thank you Fred!

Problem solved by adding @clients = Client.find(:all, :order=\>“name”)  
to my Create method. I was not considering that errors from Create  
needed to be redisplayed and collection\_select is part of that form  
that is redisplayed and it expects a @clients instance variable.

Appreciate the help.

Jeff  
On Dec 23, 2:21 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
