implementing find_or_create_by when find involves single attribute but create involves multiple attributes

Viewed 1326

In my Rails app I want to find a user by their email, or if the user does not exist, create one with that email as well as a few other attributes. Is there a less clunky way than the code below to do this, perhaps using find_or_create_by?

def set_user
  if self.user = User.find_by(email: recipient)
  else
    self.user = User.create(email: recipient,
                        password: Devise.friendly_token.first(6),
                        confirmed_at: DateTime.now)
  end
end
3 Answers
Related