title: Rails database undefined nil
body: <p>I need to adjust an old rails project.</p>
  
  <p>It generates an excel file from a database.</p>
  
  <p>A contact can be linked to a company or not.
  If it is linked to the company and defaultaddress is set on the company the excel sheet only prints the contact info of the company.</p>
  
  <p>What I need to do is that in this case I need to export both the private contact info plus the company info to the excel sheet.</p>
  
  <p>I found the file where the excel generation takes place and simply wanted to write something like this:</p>
  
  <pre><code>&lt;% @defaultaddress_id = Contact.find(qc.contact.id).defaultaddress_id %&gt;
  &lt;% @address = Address.find(@defaultaddress_id) unless @defaultaddress.empty? %&gt;
  &lt;%= @address %&gt;
  </code></pre>
  
  <p>(I tested just printing the defaultaddress_id, and this is just fine.)</p>
  
  <p>But I find this in the logs;</p>
  
  <pre><code>ActionView::TemplateError (undefined method `empty?' for nil:NilClass) on line #87 of app/views/query/_report.html.erb:
  </code></pre>
  
  <p>So I tried with following;</p>
  
  <pre><code>&lt;% @address = Address.find(@defaultaddress_id) unless @defaultaddress_id == 0 %&gt;
  </code></pre>
  
  <p>But now it sais </p>
  
  <pre><code>ActionView::TemplateError (Couldn't find Address without an ID) on line #87 of app/views/query/_report.html.erb:
  </code></pre>
  
  <p>Isn't that exactly what I am writing? Don't do the method unless there is something in the FK field?</p>
  
  <p>The defaultaddress_id is the FK of the Adress table.</p>