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! -- Posted via http://www.ruby-forum.com/.
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).displayYou''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.> :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.It does. >> nil.to_s => "" So, something else is wrong somewhere...> > def display > field.to_s > end > > > Thanks! > -- > Posted via http://www.ruby-forum.com/. > > >
Philip Hallstrom 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. -- Posted via http://www.ruby-forum.com/.
On Jul 21, 11:42 am, Cs Webgrl <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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?
James Byrne wrote:> What does the application stack trace say?Is there an easy way to generate this from a test? -- Posted via http://www.ruby-forum.com/.
Cs Webgrl wrote:> James Byrne 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 marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/.
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? -- Posted via http://www.ruby-forum.com/.