Solved

checking nil object

Posted on 2007-11-15
2
672 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Rails 3.0 Routing 3 622
Ruby problem with blocks 5 313
Determining whether a check_box is checked using javascript 2 517
searching using regular expressions 6 123
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…
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…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

895 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

11 Experts available now in Live!

Get 1:1 Help Now