How do I check if an instance has an object to skip displaying the values?

Posted by Angela on Stack Overflow See other posts from Stack Overflow or by Angela
Published on 2010-05-21T03:26:42Z Indexed on 2010/05/21 3:30 UTC
Read the original article Hit count: 216

I have created a polymorphic association around a model called status.

Some contacts will have a status associated with it. Many won't.

If I try to call a status when one is not there, I get an error. Right now, even if I haven't created a status for the model, it still runs whatever is in the if-end block.

Here's what I am trying, but it's not working:

<% if [email protected]? %>
       <p>Status: <%= @status.find(:last).status %></p>
<% end %>

In the controller, it is defined below:

@status = Contact.find(@contact).statuses

By the way, also open to make code more readable and DRY.

© Stack Overflow or respective owner

Related posts about polymorphic-associations

Related posts about nil