# Validates causes NoMethodError

**URL:** <https://www.ruby-forum.com/t/validates-causes-nomethoderror/69838>\
**Category:** Rails\
**Created:** [September 24, 2006, 10:18pm UTC](https://www.ruby-forum.com/t/validates-causes-nomethoderror/69838 "2006-09-24T22:18:32Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![simplydope](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d26b3c/32.png) [@simplydope](https://www.ruby-forum.com/u/simplydope)\
**Post date:** [September 24, 2006, 10:18pm UTC](https://www.ruby-forum.com/t/validates-causes-nomethoderror/69838/1 "2006-09-24T22:18:32Z")

</div>

I am trying to validate the uniqueness of a field:

class Department \< ActiveRecord::Base  
belongs\_to :company  
belongs\_to :region  
has\_many :employees  
validates\_uniqueness\_of :dept\_name  
end

When I run it, I get this error message:

NoMethodError in Department#create

Showing app/views/department/\_form.rhtml where line #34 raised:

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

Extracted source (around line #34):

31: \<% if @page\_title == “New Department” %\>  
32:   
33: Select One  
34: \<% @companies.each do |company| %\>  
35:   
36: \<%= company.comp\_name %\>  
37:

However, if I take validates\_uniqueness\_of off, i.e.:

class Department \< ActiveRecord::Base  
belongs\_to :company  
belongs\_to :region  
has\_many :employees  
#validates\_uniqueness\_of :dept\_name  
end

everything works just fine. What am I doing wrong?

George

---

<div class="post-metadata">

**Author:** ![simplydope](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d26b3c/32.png) [@simplydope](https://www.ruby-forum.com/u/simplydope)\
**Post date:** [September 24, 2006, 11:04pm UTC](https://www.ruby-forum.com/t/validates-causes-nomethoderror/69838/2 "2006-09-24T23:04:40Z")

</div>

Hi –

On Sun, 24 Sep 2006, simplydope wrote:

> When I run it, I get this error message:
> 
> class Department \< ActiveRecord::Base  
> belongs\_to :company  
> belongs\_to :region  
> has\_many :employees  
> #validates\_uniqueness\_of :dept\_name  
> end
> 
> everything works just fine. What am I doing wrong?

I don’t know, but I have a couple of follow-up questions/suggestions:

Is there anything in the stack trace that might help (including  
anything indicating a problem with validation itself)?

Try doing stuff in the application console. That might be a good way  
to walk through some collection-finding mini-scenarios.

If it remains a problem, post your Company model code and the view for  
that action.

David

–  
David A. Black | removed\_email\_address@domain.invalid  
Author of “Ruby for Rails” [1] | Ruby/Rails training & consultancy [3]  
DABlog (DAB’s Weblog) [2] | Co-director, Ruby Central, Inc. [4]  
[1] [Ruby for Rails](http://www.manning.com/black) | [3] [http://www.rubypowerandlight.com](http://www.rubypowerandlight.com)  
[2] [http://dablog.rubypal.com](http://dablog.rubypal.com) | [4] [http://www.rubycentral.org](http://www.rubycentral.org)

---

<div class="post-metadata">

**Author:** ![simplydope](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d26b3c/32.png) [@simplydope](https://www.ruby-forum.com/u/simplydope)\
**Post date:** [September 24, 2006, 11:40pm UTC](https://www.ruby-forum.com/t/validates-causes-nomethoderror/69838/3 "2006-09-24T23:40:16Z")

</div>

unknown wrote:

> Hi –
> 
> On Sun, 24 Sep 2006, simplydope wrote:
> 
> > When I run it, I get this error message:
> > 
> > class Department \< ActiveRecord::Base  
> > belongs\_to :company  
> > belongs\_to :region  
> > has\_many :employees  
> > #validates\_uniqueness\_of :dept\_name  
> > end
> > 
> > everything works just fine. What am I doing wrong?
> 
> I don’t know, but I have a couple of follow-up questions/suggestions:
> 
> Is there anything in the stack trace that might help (including  
> anything indicating a problem with validation itself)?
> 
> Try doing stuff in the application console. That might be a good way  
> to walk through some collection-finding mini-scenarios.
> 
> If it remains a problem, post your Company model code and the view for  
> that action.
> 
> David
> 
> –  
> David A. Black | removed\_email\_address@domain.invalid  
> Author of “Ruby for Rails” [1] | Ruby/Rails training & consultancy [3]  
> DABlog (DAB’s Weblog) [2] | Co-director, Ruby Central, Inc. [4]  
> [1] [Ruby for Rails](http://www.manning.com/black) | [3] [http://www.rubypowerandlight.com](http://www.rubypowerandlight.com)  
> [2] [http://dablog.rubypal.com](http://dablog.rubypal.com) | [4] [http://www.rubycentral.org](http://www.rubycentral.org)

## Not exactly sure what I can “do” in the colsole. Pretty new to RoR. But here are model and view:

class Company \< ActiveRecord::Base  
has\_many :departments  
has\_many :employees  
validates\_presence\_of :comp\_name  
validates\_uniqueness\_of :comp\_name  
end

* * *

new.html (department)

\<%  
@page\_title = “New Department”  
%\>

\<%= start\_form\_tag :action =\> ‘create’ %\>  
\<%= render :partial =\> ‘form’ %\>

\<%= end\_form\_tag %\>

* * *

\_form.rhtml (department)

\<%= error\_messages\_for ‘department’ %\>

| |
| 
\<%=@page\_title %\> 
 |
| |
| |
| Dept. #: | \<%= text\_field 'department', 'dept\_num' %\> |
| Dept. Name: | \<%= text\_field 'department', 'dept\_name' %\> |
| Company: | \<% if @page\_title == "New Department" %\> Select One \<% @companies.each do |company| %\> \<%= company.comp\_name %\> \<% end %\> \<% else %\> \<% @companies.each do |company| %\> \> \<%= company.comp\_name %\> \<% end %\> \<% end %\> |
| Region: | \<% if @page\_title == "Edit Department" %\> \<% @regions.each do |region| %\> \> \<%= region.region %\> \<% end %\> \<% else %\>

```
   <select name="department[region_id]">
     <% @regions.each do |region| %>
       <option value="<%= region.id %>">
         <%= region.region %>
       </option>
     <% end %>
   </select>
   <% end %>
 </td>

```

 |
| Address 1: | \<%= text\_field 'department', 'address1' %\> |
| Address 2: | \<%= text\_field 'department', 'address2' %\> |
| City: | \<%= text\_field 'department', 'city' %\> |
| State: | \<%= text\_field 'department', 'state' %\> |
| Zip Code: | \<%= text\_field 'department', 'zip\_code' %\> |
| | Frontline |
| |
| \<%= link\_to 'Back', :action =\> 'list' %\> |

* * *

Thanks for the help

George

---

<div class="post-metadata">

**Author:** ![simplydope](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d26b3c/32.png) [@simplydope](https://www.ruby-forum.com/u/simplydope)\
**Post date:** [September 25, 2006, 12:34am UTC](https://www.ruby-forum.com/t/validates-causes-nomethoderror/69838/4 "2006-09-25T00:34:47Z")

</div>

On 24 Sep 2006, at 21:13, simplydope wrote:

> NoMethodError in Department#create
> 
> Showing app/views/department/\_form.rhtml where line #34 raised:
> 
> 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.each  
> …  
> 34: \<% @companies.each do |company| %\>

Seems to me that you don’t have an @companies instance variable in  
whichever view includes \_form. So is there a path in your department  
controller that can use the \_form, without first initialising  
@companies?

Paul

---

<div class="post-metadata">

**Author:** ![simplydope](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d26b3c/32.png) [@simplydope](https://www.ruby-forum.com/u/simplydope)\
**Post date:** [September 25, 2006, 1:23am UTC](https://www.ruby-forum.com/t/validates-causes-nomethoderror/69838/5 "2006-09-25T01:23:52Z")

</div>

Paul L. wrote:

> On 24 Sep 2006, at 21:13, simplydope wrote:
> 
> > NoMethodError in Department#create
> > 
> > Showing app/views/department/\_form.rhtml where line #34 raised:
> > 
> > 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.each  
> > …  
> > 34: \<% @companies.each do |company| %\>
> 
> Seems to me that you don’t have an @companies instance variable in  
> whichever view includes \_form. So is there a path in your department  
> controller that can use the \_form, without first initialising  
> @companies?
> 
> Paul

* * *

No exactly sure what you’re asking…but here is my  
department\_controller.rb

class DepartmentController \< ApplicationController

def index  
list  
render :action =\> ‘list’  
end

# GETs should be safe (see

[URIs, Addressability, and the use of HTTP GET and POST](http://www.w3.org/2001/tag/doc/whenToUseGet.html))  
verify :method =\> :post, :only =\> [:destroy, :create, :update],  
:redirect\_to =\> { :action =\> :list }

def list  
@department\_pages, @departments = paginate :departments, :per\_page  
=\> 20,  
:conditions =\> “deleted\_yn = 0”, :order\_by =\> “dept\_name”  
end

def show  
@department = Department.find(params[:id])  
end

def new  
@department = Department.new  
@companies = Company.find(:all, :conditions =\> “deleted\_yn = 0”,  
:order =\> “comp\_name”)  
@regions = Region.find(:all, :conditions =\> “deleted\_yn = 0”, :order  
=\> “region”)  
end

def create  
@department = Department.new(params[:department])  
if @department.save  
flash[:notice] = ‘Department was successfully created.’  
redirect\_to :action =\> ‘list’  
else  
render :action =\> ‘new’  
end  
end

def edit  
@department = Department.find(params[:id])  
@companies = Company.find\_all  
@regions = Region.find\_all  
end

def update  
@department = Department.find(params[:id])  
if @department.update\_attributes(params[:department])  
flash[:notice] = ‘Department was successfully updated.’  
redirect\_to :action =\> ‘list’  
else  
render :action =\> ‘edit’  
end  
end

def destroy  
@department = Department.find(params[:id])  
Department.update(params[:id], {:deleted\_yn =\> 1})  
redirect\_to :action =\> ‘list’  
end  
end

Thanks,

George

---

<div class="post-metadata">

**Author:** ![simplydope](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d26b3c/32.png) [@simplydope](https://www.ruby-forum.com/u/simplydope)\
**Post date:** [September 25, 2006, 10:33am UTC](https://www.ruby-forum.com/t/validates-causes-nomethoderror/69838/6 "2006-09-25T10:33:42Z")

</div>

On 25 Sep 2006, at 00:23, George wrote:

> Paul L. wrote:
> 
> > On 24 Sep 2006, at 21:13, simplydope wrote:
> > 
> > > NoMethodError in Department#create

Note that the error is in create.

> > controller that can use the \_form, without first initialising  
> > @companies?

> 0", :order  
> =\> “region”)  
> end

new initialises @companies.

> def create  
> @department = Department.new(params[:department])  
> if @department.save  
> flash[:notice] = ‘Department was successfully created.’  
> redirect\_to :action =\> ‘list’  
> else  
> render :action =\> ‘new’  
> end  
> end

But create doesn’t.

The thought about model associations causing the problem is a red  
herring; copy your '@companies = ’ line into create from new, and it  
should work.

Paul

---

<div class="post-metadata">

**Author:** ![simplydope](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d26b3c/32.png) [@simplydope](https://www.ruby-forum.com/u/simplydope)\
**Post date:** [October 16, 2006, 3:16am UTC](https://www.ruby-forum.com/t/validates-causes-nomethoderror/69838/7 "2006-10-16T03:16:39Z")

</div>

Paul L. wrote:

> On 25 Sep 2006, at 00:23, George wrote:
> 
> > Paul L. wrote:
> > 
> > > On 24 Sep 2006, at 21:13, simplydope wrote:
> > > 
> > > > NoMethodError in Department#create
> 
> Note that the error is in create.
> 
> > > controller that can use the \_form, without first initialising  
> > > @companies?
> 
> > 0", :order  
> > =\> “region”)  
> > end
> 
> new initialises @companies.
> 
> > def create  
> > @department = Department.new(params[:department])  
> > if @department.save  
> > flash[:notice] = ‘Department was successfully created.’  
> > redirect\_to :action =\> ‘list’  
> > else  
> > render :action =\> ‘new’  
> > end  
> > end
> 
> But create doesn’t.
> 
> The thought about model associations causing the problem is a red  
> herring; copy your '@companies = ’ line into create from new, and it  
> should work.
> 
> Paul

Thanks. It worked. Here’s the result, for those that have this problem  
in the future.

def new  
@department = Department.new  
@companies = Company.find(:all, :conditions =\> “deleted\_yn = 0”,  
:order =\> “comp\_name”)  
@regions = Region.find(:all, :conditions =\> “deleted\_yn = 0”, :order  
=\> “region”)  
end

def create  
@department = Department.new(params[:department])  
@companies = Company.find(:all, :conditions =\> “deleted\_yn = 0”,  
:order =\> “comp\_name”)  
@regions = Region.find(:all, :conditions =\> “deleted\_yn = 0”, :order  
=\> “region”)  
# I could use this for deleted\_yn  
# @department.deleted\_yn = 0  
if @department.save  
flash[:notice] = ‘Department was successfully created.’  
redirect\_to :action =\> ‘list’  
else  
render :action =\> ‘new’  
end  
end
