# NoMethodError

**URL:** <https://www.ruby-forum.com/t/nomethoderror/69809>\
**Category:** Ruby\
**Created:** [September 24, 2006, 8:10am UTC](https://www.ruby-forum.com/t/nomethoderror/69809 "2006-09-24T08:10:49Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [September 24, 2006, 8:10am UTC](https://www.ruby-forum.com/t/nomethoderror/69809/1 "2006-09-24T08:10:49Z")

</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\_presence\_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:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [September 24, 2006, 7:38pm UTC](https://www.ruby-forum.com/t/nomethoderror/69809/2 "2006-09-24T19:38:13Z")

</div>

On Sun, Sep 24, 2006 at 03:10:06PM +0900, removed\_email\_address@domain.invalid wrote:

> I am trying to validate the uniqueness of a field:  
> [snip]

Well first, you are posting to the wrong mailing list ;). You want the  
Rails mailing list for this question. Second, I’m guessing that the  
table column isn’t actually dept\_name (maybe a typo, dep\_name, maybe you  
actually spelled it out department\_name, who knows). But that’s just a  
guess. I would suggest asking your question again on the rails list.
