handling of nil objects

Using a method in your User model in place of accessing the relationship's attributes is probably the best way to handle this.

####User Model class User < ActiveRecord::Base   belongs_to :department

  def department_name     department.nil? ? '' : department.name   end end

Whenever you need the department name use that method user.department_name. Extending this a bit, you could open it up for all fields (renamed to prevent a conflict with the real attr_accessor for department):

  def department_attribute(field)     department.nil? ? '' : department.send(field)   end

Then you can do user.department_attribute(:name).

Or, you could turn off whiny nils (see config/environments/development.rb), at the expense of obscuring bugs.

You could also do stuff like this...

@department.department_name rescue "None"

or override the reader for the attribute...

class User def department_name   self[:department_name] || "None" end end