# Empty string and nil values help needed

**URL:** <https://www.ruby-forum.com/t/empty-string-and-nil-values-help-needed/172435>\
**Category:** Rails\
**Created:** [July 21, 2009, 4:18pm UTC](https://www.ruby-forum.com/t/empty-string-and-nil-values-help-needed/172435 "2009-07-21T16:18:46Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![cswebgrl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/edb3f5/32.png) [@cswebgrl](https://www.ruby-forum.com/u/cswebgrl)\
**Post date:** [July 21, 2009, 4:18pm UTC](https://www.ruby-forum.com/t/empty-string-and-nil-values-help-needed/172435/1 "2009-07-21T16:18:46Z")

</div>

I have done many variations of this code. Essentially I want this to be  
true.

assert\_equal ‘’, Org.field\_for(nil).display

This is a polymorphic relationship.

# fieldable.rb

def field\_for(purpose)  
self.fields.find(  
:first,  
:conditions =\> [“purpose LIKE ?”, “#{purpose}”],  
:order =\> ‘position asc’)  
end # def

# field.rb

def display  
str = “”  
field.to\_s unless search.blank?  
str  
end

Can someone help tell me what I’m doing wrong and why I can’t produce an  
empty string? I had thought that nil.to\_s created an empty string, but  
just writing the following didn’t work either.

def display  
field.to\_s  
end

Thanks!

---

<div class="post-metadata">

**Author:** ![cswebgrl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/edb3f5/32.png) [@cswebgrl](https://www.ruby-forum.com/u/cswebgrl)\
**Post date:** [July 21, 2009, 5:18pm UTC](https://www.ruby-forum.com/t/empty-string-and-nil-values-help-needed/172435/2 "2009-07-21T17:18:08Z")

</div>

On Jul 21, 2009, at 7:18 AM, Cs Webgrl wrote:

> I have done many variations of this code. Essentially I want this  
> to be  
> true.
> 
> assert\_equal ‘’, Org.field\_for(nil).display

You’ve overridden it so I doubt this is the problem, but display is a  
built-in method at a very basic level (somewhere near inspect) so  
maybe that’s messing you up in case you aren’t overriding it correctly.

> This is a polymorphic relationship.
> 
> # fieldable.rb
> 
> def field\_for(purpose)  
> self.fields.find(  
> :first,  
> :conditions =\> [“purpose LIKE ?”, “#{purpose}”],

Are you sure you didn’t mean [“purpose LIKE ?”, purpose] There’s no  
reason to quote the second argument. Although I don’t know what Rails  
does when you pass in nil to conditions like that. Maybe it doesn’t  
like that.

> str  
> end
> 
> Can someone help tell me what I’m doing wrong and why I can’t  
> produce an  
> empty string? I had thought that nil.to\_s created an empty string,  
> but  
> just writing the following didn’t work either.

It does.

> > nil.to\_s  
> > =\> “”

So, something else is wrong somewhere…

---

<div class="post-metadata">

**Author:** ![cswebgrl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/edb3f5/32.png) [@cswebgrl](https://www.ruby-forum.com/u/cswebgrl)\
**Post date:** [July 21, 2009, 5:42pm UTC](https://www.ruby-forum.com/t/empty-string-and-nil-values-help-needed/172435/3 "2009-07-21T17:42:55Z")

</div>

Philip H. wrote:

> You’ve overridden it so I doubt this is the problem, but display is a  
> built-in method at a very basic level (somewhere near inspect) so  
> maybe that’s messing you up in case you aren’t overriding it correctly.

I changed the method name to display\_field, but the test still fails.

> Are you sure you didn’t mean [“purpose LIKE ?”, purpose] There’s no  
> reason to quote the second argument. Although I don’t know what Rails  
> does when you pass in nil to conditions like that. Maybe it doesn’t  
> like that.

I modified this also and the test passes/fails both ways.

> > > nil.to\_s  
> > > =\> “”
> 
> So, something else is wrong somewhere…

Tests run on this method work fine if I’m not passing in a nil value.  
I’m not quite sure where else to look. Any thoughts?

Thanks.

---

<div class="post-metadata">

**Author:** ![cswebgrl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/edb3f5/32.png) [@cswebgrl](https://www.ruby-forum.com/u/cswebgrl)\
**Post date:** [July 21, 2009, 7:24pm UTC](https://www.ruby-forum.com/t/empty-string-and-nil-values-help-needed/172435/4 "2009-07-21T19:24:12Z")

</div>

James B. wrote:

> What does the application stack trace say?

Is there an easy way to generate this from a test?

---

<div class="post-metadata">

**Author:** ![cswebgrl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/edb3f5/32.png) [@cswebgrl](https://www.ruby-forum.com/u/cswebgrl)\
**Post date:** [July 21, 2009, 7:47pm UTC](https://www.ruby-forum.com/t/empty-string-and-nil-values-help-needed/172435/5 "2009-07-21T19:47:28Z")

</div>

Cs Webgrl wrote:

> James B. wrote:
> 
> > What does the application stack trace say?
> 
> Is there an easy way to generate this from a test?

The trace should be in test.log.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![cswebgrl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/edb3f5/32.png) [@cswebgrl](https://www.ruby-forum.com/u/cswebgrl)\
**Post date:** [July 21, 2009, 9:25pm UTC](https://www.ruby-forum.com/t/empty-string-and-nil-values-help-needed/172435/6 "2009-07-21T21:25:26Z")

</div>

Marnen Laibow-Koser wrote:

> The trace should be in test.log.

This code is in a plugin. To run tests, I’m running them directly out  
of the vendor/plugins directory. Any ideas how to write a test.log  
there?

---

<div class="post-metadata">

**Author:** ![cswebgrl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/edb3f5/32.png) [@cswebgrl](https://www.ruby-forum.com/u/cswebgrl)\
**Post date:** [July 21, 2009, 7:12pm UTC](https://www.ruby-forum.com/t/empty-string-and-nil-values-help-needed/172435/7 "2009-07-21T19:12:11Z")

</div>

On Jul 21, 11:42 am, Cs Webgrl [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Tests run on this method work fine if I’m not passing in a nil value.  
> I’m not quite sure where else to look. Any thoughts?

What does the application stack trace say?
