Solved

checking nil object

Posted on 2007-11-15
2
679 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Article by: narshlob
If you've ever programmed in Ruby and have come across either a proc or a lambda, you might have been wondering what the difference is between the two and when you would use one over the other. This article will try to explain the difference between…
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…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

623 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