title: Why am I getting undefined method `first' for nil:NilClass when trying to pull value from array in ruby on rails?
body: <p>I created arrays some months back for dealing with the editing of user profile details. When displaying back users registered countries I tap into the array and get the result I want using the stored integers from the db.</p>
  
  <p><strong>In my User model I have full list of countries stored in an array:</strong></p>
  
  <pre><code># encoding: UTF-8
  class User &lt; ActiveRecord::Base   
  
       COUNTRY_AND_ISO_CODE   = [
            ['Any', nil],
            ['United Kingdom', 826],
            ['United States', 840],
            ['-----------', ' '],
            ['Afghanistan', 4],
            ['Ãland Islands', 248],
            ['Albania', 8],
            ['Algeria', 12],
            ['American Samoa', 16],
            ['Andorra', 20],
            ['Angola', 24],
            ['Anguilla', 660],
            ['Antarctica', 10],
            ['Antigua and Barbuda', 28],
            ['Argentina', 32],
            ['Armenia', 51],
            ['Aruba', 533],
  </code></pre>
  
  <p><strong>Put this in my view:</strong></p>
  
  <pre><code>= User::COUNTRY_AND_ISO_CODE[@profile.country].first
  </code></pre>
  
  <p><strong>Shows this result in my view e.g.:</strong></p>
  
  <pre><code> United Kingdom
  </code></pre>
  
  <p>The issue I'm having is with the large country array. For some profiles it seems to work but most I get this error:</p>
  
  <pre><code> undefined method `first' for nil:NilClass
  </code></pre>
  
  <p>I know for a fact @profile.country for every profile isn't nil so not sure where this error is coming from. If I remove the call to the array and just have @profile.country in my view it shows the stored integer as expected.</p>
  
  <p>I'm not experiencing this issue with my other arrays.. e.g. martial_status, gender, sexuality. So I'm wondering whether this could be something to do with the size of the array and memory or something? I don't know that's why I'm asking.</p>
  
  <p>I'm close to taking another route which was suggested in another question I asked by creating another table with the country names stored and use the integer stored in the profiles table to get the correct country from the countries table. I wanted to avoid this though because that's 2 calls to the db when I originally had 1.</p>
  
  <p>Any idea what could be wrong with my current code? I can't figure out why it's working on some profiles and not others.  I don't need to check for a stored country integer because I created a rake task that updated every user with a country.</p>
  
  <p>Other question: <a href="https://stackoverflow.com/questions/10972302/how-do-i-display-select-field-values-rather-than-their-associated-stored-integer">How do I display select field values rather than their associated stored integers in ruby on rails?</a></p>
  
  <p>Hope you can help</p>
  
  <p>Also if you mark me down please give a reason. I'm here to learn from my mistakes and maybe my mistakes can help others who come across my question with similar issues. </p>
  
  <p>Kind regards</p>