vendredi 25 octobre 2019

Ruby check if record exists and associations in one line

Something I always run in to. I want to do this:

if current_user.exists? || current_user.role == "admin"
  # Do something
end

Obviously if current user does not exist then this does not work as it returns undefined method role for nil class

Is there a way I can achieve the above in one line without multiple If statements?

Aucun commentaire:

Enregistrer un commentaire