Rails: undefined method `strftime' for nil:NilClass, but value (without strftime) returns OK
-
13-10-2019 - |
Question
I have an odd problem with a (rails 3) activerecord query.
If I submit query #1 (below) to the view (below) everything works fine...but if I submit query #2 (below), I get "undefined method `strftime' for nil:NilClass"
...However, if I strip the strtime string away from query #2, it returns the unformatted time just fine.
"Professional specialties" is a has_many :through table; I'm assuming that has something to do with it...but I can't quite figure out what.
Any idea what's going on? All suggestions appreciated:
Query #1 (works fine)
@professionals = Professional.all(:include => :user, :conditions => ["users.first_name = ? AND users.last_name = ?", params[:name][:first], params[:name][:last]])
Query #2 (breaks strtime)
@professionals = Professional.all(:include => [:user, :professional_specialties], :conditions => ["professional_specialties.profession_type_id = ?", params[:profession_type]] )
View
<% @professionals.each do |p| %>
<tr>
<td><%= link_to("%s %s" % [p.user.first_name, p.user.last_name], p) %></td>
<td><%= p.business %></td>
<td><%= p.user.role.name %></td>
<td><%= p.user.created_at %></td>
<td><%= p.user.last_sign_in_at.strftime("%Y") %></td>
</tr>
<% end %>
Solution
My best guess would be that the two queries give you different resultsets. In the second resultset, one or more of the users has a NULL
value for the last_sign_in_at
column.
Run Professional.all(...).map(&:users)
in the Rails console (rails console
in your application directory), and double-check that they all have valid timestamps.