Solved

checking nil object

Posted on 2007-11-15
2
668 Views
Last Modified: 2012-06-21
hi,

I am wondering if there is smart/elegant ways of checking nil object.

I have this in controller:

 @attending_ip = AttendingIp.find_by_sql("SELECT .....")

In view:

<% if @attending_ip != nil%>
<% @attending_ip.each do |u|  %>
...
...

<%if u.admit_date != nil%><%= u.admit_date.to_formatted_s(:my_format_0)  %><%end%>

...
...

I am checking  whether u.admit_date is nil otherwise, it throws error message since admit_date can be null. So, do I always have to check for all the columns? Are there better ways of doing this?

thx,
0
Comment
Question by:dkim18
2 Comments
 
LVL 19

Accepted Solution

by:
peh803 earned 350 total points
ID: 20294959
With ruby, there is always a better way :-)

Try this...just a little bit cleaner.

Regards,
Phil
<%= u.admit_date.to_formatted_s(:my_format_0) if u.admit_date %>

Open in new window

0
 

Author Closing Comment

by:dkim18
ID: 31409416
Ok, that is good enough. thx!
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

In Ruby, Call or invoke a API DLL library is easily via Win32API class, win32-api gem or other gems. For general DLL API call, there are quite a few references, some good tips list below: http://www.rubytips.org/2008/05/13/accessing-windows-api-fro…
Recently I spent hours debugging an issue in a Rails project where ActiveRecord was causing MySQL errors trying to create a User object of a class at the top level of a Single Table Inheritance model structure.  It turns out `.create` behaves differ…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now