Reputation: 494
I need to adjust an old rails project.
It generates an excel file from a database.
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.
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.
I found the file where the excel generation takes place and simply wanted to write something like this:
<% @defaultaddress_id = Contact.find(qc.contact.id).defaultaddress_id %>
<% @address = Address.find(@defaultaddress_id) unless @defaultaddress.empty? %>
<%= @address %>
(I tested just printing the defaultaddress_id, and this is just fine.)
But I find this in the logs;
ActionView::TemplateError (undefined method `empty?' for nil:NilClass) on line #87 of app/views/query/_report.html.erb:
So I tried with following;
<% @address = Address.find(@defaultaddress_id) unless @defaultaddress_id == 0 %>
But now it sais
ActionView::TemplateError (Couldn't find Address without an ID) on line #87 of app/views/query/_report.html.erb:
Isn't that exactly what I am writing? Don't do the method unless there is something in the FK field?
The defaultaddress_id is the FK of the Adress table.
Upvotes: 1
Views: 46
Reputation: 33552
I believe @defaultaddress
should be @defaultaddress_id
And also use .blank?
instead to .empty?
in case of nil values.
<% @address = Address.find(@defaultaddress_id) unless @defaultaddress_id.blank? %>
nil.empty?
#=> NoMethodError: undefined method `empty?' for nil:NilClass
nil.blank?
#=> true
Upvotes: 1