Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

checking nil object

Posted on 2007-11-15
2
Medium Priority
?
681 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 1400 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I recently rediscovered rails when I needed a holiday project and decided to build a management dashboard for the company where I work.  With it being a project done in my free time, I could focus my time on learning the basics rather than trying to…
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…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Loops Section Overview
Suggested Courses
Course of the Month11 days, 4 hours left to enroll

886 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