My app has several different forms for creating user accounts and currently they all collect
last_name as separate fields but I’d like to change to a more human-friendly format of
full_name then split into two parts in the backend.
What are the Rails/Ruby idiomatic way to do this? I’m interested in hearing different methods.
I would need to validate the two fields before saving, of course, but also I’d like to be able to do some kind of on-page validation of the
full_name field to detect any potential problems with splitting, like multiple spaces in the name, and display a message to the user before the form is submitted.
It depends on how clean you expect your input to be.
A quick-and-dirty solution is to split the name at the words; take the last word for the last name, everything in front as first/middle name.
This will fail on such things as “Johann Gambolputty de von Ausfern of Ulm”, though (“de von” is properly part of the last name)
There is a People gem that purports to be able to parse names, with awareness of prefixes and suffixes:
…so you can give it something like “Dr. Fred E Smith, MD” and it’ll put all the parts in the right place, but I’ve found it chokes on anything even slightly more complex (“Rear Admiral Grace Hopper” fails to parse at all).
Why do you need to store the name as two fields? Why not ask for and store a single field?
Hacker News linked to this article yesterday, which points out the many, many dangers behind trying to parse names by looking for spaces:
Thanks all, very useful info here and in the articles linked.
I’m also a fan of the Kalzemeus blog article from 2010, I read that a few years ago and it really stuck with me.
@matthucke thanks for the gem reference, and thanks for the tip on how easily it chokes, sounds like it might actually cause more problems than it solves.
@geoffharcourt & @derekprior I’ll need to go away and think about this for a while before I make any major changes.
Two very valid points - the combination of the dangers of parsing a name and the question “in what situation would I need to display anything other than the full name?” might lead to an enlightenment moment!
I’ll have a look through the Learn repo too (should’ve done that first, of course!) and see how you guys handle it in-house. I notice that my Settings page shows my full name as one field but emails are addressed to my first name only so there must be some splitting going on somewhere I guess. Off I go to check…
I agree with @derekprior - one name field can be better.
In my case, I have a legacy application that has the first_name/last_name fields (an online shop, six years with the same java app), where the users enter the two name fields separately. Lately, I’ve had to import names from an external API (Amazon marketplace), where they provide only the concatenated name field rather than first/last, so I’ve had to investigate this; dealing with parsing names is, for now, easier than going back and fixing the legacy apps.
In all the years I’ve been maintaining this I’ve never had to do anything with the name fields other than concatenate them. Separating out first/last was a mistake.
For a new project, consider just using a full_name field and allowing anything reasonable in it, avoiding these issues entirely.
You can also do full name and nickname if you need a full name for some reason but also want a name to show on the page when you address/email a user.
Nice job everyone parsing out how large and complicated this seemingly straightforward topic can be!