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 7562997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T13:33:09+00:00 2026-05-30T13:33:09+00:00

I have a rails question that I have been unable to find an answer

  • 0

I have a rails question that I have been unable to find an answer for on my own. I apologize if it’s very simple or obvious, I’m a complete beginner here.

So I have a column in my db called :client_code, which is defined in the model as the down-cased concatenation of the first character of :first_name, and :last_name. So for instance, if I pull up a new form and enter ‘John’ for :first_name, and ‘Doe’ for :last_name, :client_code is automatically given the value of ‘jdoe’. Here’s the relevant portion of the model code:

class Client < ActiveRecord::Base

  before_validation :client_code_default_format
  validates_presence_of :first_name, :last_name, :email
  validates_uniqueness_of :client_code

  ...


  def client_code_default_format
    self.client_code = "#{first_name[0]}#{last_name}".downcase
  end
end

I would like to add something to this code so that, in the event that someone enters a different client with the same exact name, it does’t fail the uniqueness validation but instead creates a slightly modified :client_code (‘jdoe2’, for example). I could probably figure out how to add an index to all of them, but I would prefer to only include numbers as a failsafe in the case of duplicates. Can anyone point me in the right direction?

  • 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-05-30T13:33:11+00:00Added an answer on May 30, 2026 at 1:33 pm

    Many thanks to @Dieseltime for his response. I took his suggestion and was able to get the functionality I wanted with some minor changes:

    before_validation :format_client_code
    validates_presence_of :first_name, :last_name, :email, :company_id
    validates_uniqueness_of :client_code
    
    ...
    
    def format_client_code
      unless self.client_code != nil
        default_client_code = "#{first_name[0]}#{last_name}".downcase
        count = Client.count(:conditions => "client_code = '#{default_client_code}'")
        if count == 0
          self.client_code = default_client_code
        else
          self.client_code = default_client_code + (count + 1).to_s
        end
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have recently been advised that for my current rails app relationships I should
The rails books and web pages I've been following have all stuck to very
First of all, let me say I am very new to rails, have been
A question has many answers. It is easy to find all questions that have
I'm aware that variants of this question have been asked before, but I do
I have a syntax question regarding the Rails 3 ActiveRecord::Base.connection.execute method and parameter that
I have a rails app running on port 3000. The page in question has
Im new to rails and have a question about accessing relations of relations in
A bit of a newbie question on rails associations. I have a Bug model,
I have a question about ruby on rails and the process of assigning variables

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.