I'm new to Ruby, so I'm sure this is an easy one.
I have the following class:
When I call the full_name method from a web page, it returns the full name as expected. But when I call if from the to_s method, it returns an empty string because first_name, last_name, and middle_name are empty strings. Here's the console output:Code:class Pilot < ActiveRecord::Base validates_presence_of :last_name, :first_name, :middle_name, :birth_date has_many :flights, :dependent => :destroy has_many :aircraft def flight_count return flights.length end def full_name puts "full_name()" name = "" if @first_name != nil then name += @first_name end puts "@first_name = \"#{@first_name}\" name = \"#{name}\"" if @middle_name != nil then name += " " + @middle_name end puts "@middle_name = \"#{@middle_name}\" name = \"#{name}\"" if @last_name != nil then name += " " + @last_name end puts "@last_name = \"#{@last_name}\" name = \"#{name}\"" puts "full_name() returns \"#{name}\"" return name end def inspect return to_s end def to_s return "Pilot [#{id}](name = \"#{self.full_name}\" birth_date = " + (birth_date == nil ? "nil" : birth_date.to_formatted_s(:short)) + " flights = " + flight_count.to_s + ")" end end
Any ideas what I'm doing wrong?Code:C:\Ruby\projects\logbook>ruby script/server => Booting Mongrel => Rails 2.3.5 application starting on http://0.0.0.0:3000 => Call with -d to detach => Ctrl-C to shutdown server new() pilot_id: "2" full_name() @first_name = "" name = "" @middle_name = "" name = "" @last_name = "" name = "" full_name() returns "" pilot = "Pilot [2](name = "" birth_date = 12 Oct flights = 1)" full_name() @first_name = "" name = "" @middle_name = "" name = "" @last_name = "" name = "" full_name() returns "" pilot = "Pilot [2](name = "" birth_date = 12 Oct flights = 1)" full_name() @first_name = "" name = "" @middle_name = "" name = "" @last_name = "" name = "" full_name() returns "" Processing FlightsController#new (for 127.0.0.1 at 2010-04-13 21:46:55) [GET] Parameters: {"pilot_id"=>"2"} ←[4;36;1mPilot Load (0.0ms)←[0m ←[0;1mSELECT * FROM "pilots" WHERE ("pilots" ."id" = 2) ←[0m ←[4;35;1mFlight Load (0.0ms)←[0m ←[0mSELECT * FROM "flights" WHERE ("flights ".pilot_id = 2) ←[0m Rendering template within layouts/flights Rendering flights/new Rendered flights/_form (1047.0ms) Completed in 2422ms (View: 1359, DB: 0) | 200 OK [http://localhost/pilots/2/flig hts/new]




Bookmarks