Forum: Ruby Checking Join data for nil

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
2a84ec1a0c6e96a8d80bd9f569c80322?d=identicon&s=25 Justin Kay (jkay)
on 2006-01-11 17:58
So I have a join 1:0..1.  If there is nothing in the child side I get an
error when I say property.pdf.name  that I can't access nil.name.  But I
can't seem to figure out how to check if pdf is nil or not before I call
the .name function.  If I test for property.pdf == nil it always comes
back as nil, which it isn't.

Any ideas?
4b174722d1b1a4bbd9672e1ab50c30a9?d=identicon&s=25 Ryan Leavengood (Guest)
on 2006-01-11 22:12
(Received via mailing list)
On 1/11/06, Justin Kay <jkay@2pattersons.com> wrote:
> So I have a join 1:0..1.  If there is nothing in the child side I get an
> error when I say property.pdf.name  that I can't access nil.name.  But I
> can't seem to figure out how to check if pdf is nil or not before I call
> the .name function.  If I test for property.pdf == nil it always comes
> back as nil, which it isn't.
>
> Any ideas?

I'm not exactly sure what you are doing, but in general to check for
nil you can just use "if" like so (nil is false):

if property.pdf
  puts property.pdf.name
end

Or if you want to be explicit:

unless property.pdf.nil?
  puts property.pdf.name
end

If property.pdf is always nil, then you have some other bug in your
code.

Ryan
This topic is locked and can not be replied to.