In my app I'm trying to automatically populate a username
column from the first_name
field that I already have tied in to my Devise
login system. In theory, it should just be the user
's first_name
if they are the only one with that name, but it should be something like "Mal the 4th" or "Jayne the 3rd" if there are other users
with that first name already.
So far, in googling and consulting other SO posts (like this one) I have this basic structure in my registrations_controller
:
before_create :set_username
private
def set_username
if self.first_name ...isn't the same as that of other users...
self.username = first_name
else
self.username = first_name + " the " + ordinalize(...something...)
end
end
But I'm struggling to fill in the blanks. So far it looks like the best way to do it is to do an if statement that checks if that first_name
is unique and, if it's not to ordinalize
some kind of count
, but please let me know if there's a better or "more Ruby" way. Any help getting this to work would be appreciated!
Aucun commentaire:
Enregistrer un commentaire