NoMethodError

G

gpasley

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: <select name="department[company_id]">
33: <option value="0">Select One</option>
34: <% @companies.each do |company| %>
35: <option value="<%= company.id %>">
36: <%= company.comp_name %>
37: </option>

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
 
L

Logan Capaldo

I am trying to validate the uniqueness of a field: [ snip ]
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?
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,234
Latest member
SkyeWeems

Latest Threads

Top