Validates causes NoMethodError

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: <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

Hi --

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: <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?

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

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

Paul Lynch wrote:

NoMethodError in Department#create

Note that the error is in 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?

No exactly sure what you're asking...but here is my department_controller.rb

class DepartmentController < ApplicationController ...   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

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