Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7831301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T11:39:59+00:00 2026-06-02T11:39:59+00:00

In a Rails model, if I have a field that is for proper names,

  • 0

In a Rails model, if I have a field that is for proper names, what is the best practice way to ensure they are uniformly capitalized despite potentially lazy input from users?

IE let’s say the model is Contact and the field is Name. No matter what the user inputs I want the words to be capitalized, ie the model would tranform the following input to the following output:

john doe -> John Doe
j doe -> J Doe
John doe -> John Doe
john doe jr -> John Doe Jr

So, do you create a before_save callback and tranform the field with a regex, or do you create some kind of validation, or something different? I’d very much appreciate answers with an emphasis on the rationale, of why you would do it one way or another, because that’s what I’m most stuck on.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T11:40:00+00:00Added an answer on June 2, 2026 at 11:40 am

    The method you want is titleize. The capitalize method only does the first character in the entire string so "john doe".capitalize #=> "John doe", "john doe".titleize #=> "John Doe".

    As for how to implement it, you could go with a before_save, but I like to do these type of simple preparations of single attributes with pseudo-attributes. Basically, you define a writer method for an attribute that will hide the default one created by ActiveRecord. You’ll still be able to access the attribute using the write_attribute method (docs). Doing it this way will ensure that the attribute is set properly as soon as you assign it, meaning that even before you save it, you can be sure your object is in a valid state:

    class Contact < ActiveRecord::Base
    
      def name=(s)
        write_attribute(:name, s.to_s.titleize) # The to_s is in case you get nil/non-string
      end
    
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a field in my Rails model that has max length 255. I'm
I have a Ruby on Rails model that has a column called expiration_date .
Using Mongoid 2.4.5 on Rails 3.2.1 I have a Model Book that has_many :pages
I have a rails app (rails 3.1.3) that has a shopping cart model. I
I have a model and in that model I'm generating a more complex field
I have a rails model that I am adding validations to and I seem
I have an active record Song model with a songhash field (string(255)) that contains
I have a model Token, which has a field token_number that I need to
We have a very simple payments model with the default created_at datetime field that
I'm on Rails 3. I have a model Client that has an address_id .

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.