Handling of nil objects

to paint the picture

I currently have a system which lists uses and the Department the user
is assigned to. So my models look something like this.

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

####Department Model
class Department < ActiveRecord::Base
belongs_to :organization
has_many :users
end

A user may not necessarily have a department which is fine however, when
this user does not have a department this department value will be nil.
When I go to print out this department value I get the following error

You have a nil object when you didn’t expect it!
The error occured while evaluating nil.name

the name value is my department name. What’s the best way to tell rails
to print out nothing if this object is set to nil?

I was thinking of setting up a helper method. But I’m not sure that
that is the best thing to do.

Thanks in advance for any help.

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.

On Oct 14, 9:20 am, Stewart [email protected] wrote:

end

Posted viahttp://www.ruby-forum.com/.
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

Eden Li wrote:

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.

That’s perfect! I ended up defining a method in the user model. This
will help me with a number of other issues I was facing. I did not
fully understand implementing methods in our model before. I was just
used to declaring them for my controllers. Thank you so much for your
help!

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs