Search code examples
ruby-on-railsibm-midrangenullnomethoderrordb2-400

Ruby on Rails Testing for Nil - NoMethodError Nil:class


We've been continuously working on this issue for a few months and not getting far with it. Since this was first asked, we changed the code (based on what the original developer for the site suggested), but we are still not getting where we need to be.

I'm relatively new to Ruby and am currently taking some courses to learn more about it, so please bear with me. We're using Ruby 1.9 and Rails 3.2 We use AS 400/DB2 for our database.

We have an online ordering site that you have to have an account set up to access. Depending on what type of account you are set up as, you might have to have your order approved by someone. I.e. if I am a drop ship account, my distributor has to approve what I'm ordering. Also if I am a dedicated drop ship account, my distributor has to approve what I'm ordering as well.

        if sign_on.acctypw1.strip == "DS" or sign_on.acctypw1.strip == "DSD"        
            rec = SignOn.first(:conditions => {:userw1 => login}, :select=> "prfdstw1")      
                #ignore_exception { puts "webid = rec.prfdstw1"; raise Exception; puts "webid = rec.prfdstw1";}   
                webid = "rec.prfdstw1"
                def ignore_exception
                   begin
                     yield  
                   rescue Exception
                  end
                end   

        if sign_on.acctypw1.strip == "DS"   
             rec = SignOn.find_by_userw1(login)            
            webid = rec.prfdstw1 

        end 

        elsif sign_on.acctypw1.strip == "DSD"   
             rec = SignOn.find_by_userw1(login)            
            webid = rec.prfdstw1 
        end 

We are getting the approval emails fine but we noticed that when there is not an email address for a dedicated drop ship for example, that we are getting a NOMethodError (undefined method 'prfdstw1' for nil:NilClass)

My guess is that I need a way to test for Nil, what is the best way to do that in ROR?

UPDATE---------------- the error that I get for some DS accounts is:

NoMethodError (undefined method `prfdstw1' for nil:NilClass): 
  app/models/order.rb:355:in `submit_order'

Line #355

target_email = Contact.connection.select_values
("SELECT EMAL23 FROM WEBOEL23 WHERE ACT223 = #
{Contact.connection.quote(rec.prfdstw1)}").uniq

on further testing, it appears that the failures occur on accounts that have more than 2 approval emails.

my theroy now is that there is an issue with the way I am doing the array

                if sign_on.acctypw1.strip == "DS" or sign_on.acctypw1.strip == "DSD" 
                    # If there is no distributor email address, the mailer model will substitute in the admin's email from their settings
                     target_email.each do | email_address | Mailer.deliver_order_distributor_approval_email(email_address, 'Coastal Pet Online Ordering<[email protected]>', "Order Confirmation Approval", email_details)
                     end

is there a better way to return the array taht would allow for more than 2 emails?


Solution

  • It depends to what you want.

    You can use this:

    webid = rec.prfdstw1 if rec
    

    Or this:

    webid = rec ? rec.prdfdstw1 : "null"