I have two methods which are used to determine whether to apply a class to the page to show that something is overdue and needs attention.
I’m getting an error when a brand new user registers:
undefined method `last_contact_done_date=' for #<User:0x6183708>
The line that it’s referencing the error to is this:
2: <div class="span3 <%= "overdue" if (signed_in? && contact_overdue?(current_user.id)) %>">
The contact_overdue? method is this (in a home page helper)
def contact_overdue?(user_id)
@user = User.find_by_id(user_id)
return true if (Date.today - (@user.last_contact_done_date ||= Date.tomorrow)) > 6
end
and the last_contact_done_date method is this in the User model
def last_contact_done_date
self.contacts.order('date_done DESC').first.try(:date_done)
end
I thought that if I was using the ||= operator in the contact_overdue? method, then I would return -1 if the last_contact_done_date is nil. But it appears that’s not working. What operator should I be using in last_contact_done_date or how should I change the contact_overdue? method so that if there are no contacts, then false is returned from the contact_overdue? method?
To return the default value of
-1when there is no last contact done date, use(it is unreasonable to expect
Date.tomorrowto return-1😉 )||=is an assignment operator;a ||= bis equivalent toa = a || b; and ifais an attribute (i.e. is prefixed with a dot and an instance,c.a), assignment to it will call the methoda=. Thus, your code necessitates that you have a method namedlast_contact_done_date=to handle the assignment, which you don’t.