Empty string and nil values help needed

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!

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…

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.

James B. wrote:

What does the application stack trace say?

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

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
[email protected]

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?

On Jul 21, 11:42 am, Cs Webgrl [email protected]
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?