Dealing with nil relation

I have a person table and a relation to a table for absents. When
listing absents I get the name by the relationship and the person_id.
But now I get an error because the person is deleted. How do I check if
a relation is missing?

you can check for nil of course…

eg:
<%= @absent.person.name if @absent.person %>

But that is weak db/model design.

-Either don’t allow deletion of persons and use an is_active flag
instead,
(most likely the best way to handle this)

  • or make sure that if you delete a person all related records get
    deleted too.
    You can do this manually in the destroy action of the
    PersonsController
    or by using :dependent => destroy for all the associations.