jeudi 28 janvier 2016

Rails / simplifying syntax when comparing nil or not elements

having the following condition, where invitation_sent_at can be nil :

if self.invitation_last_sent_at + 1.hour
    #then do something
end

I end up writing the following :

if (self.invitation_sent_at.presence && (self.invitation_sent_at + 1.hour) ...

I'm wondering if there is a cleaner syntax to handle the fact that self.invitation_sent_at could be nil, but without having to verify it so verbosely if it is present

Aucun commentaire:

Enregistrer un commentaire