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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:42:53+00:00 2026-06-15T18:42:53+00:00

I try to construct a username based on name given. Since many people are

  • 0

I try to construct a username based on name given. Since many people are named “John” I need to somehow check for this and create it by following a +1 count.

I’m a bit lost in how to iterate ActiveRecord to find in this case a username called john, then if already exists try john1 and if not available try john2 and so on. I can suppose it would use while but I have no idea how to iterate it for this case.

Any idea how to do this?

  • 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-15T18:42:53+00:00Added an answer on June 15, 2026 at 6:42 pm

    In order to not make multiple trips to the database, get a list of users that match the name, check the count of users, append the count to the name, and then check the already loaded collection for the new name.

    It would look something like this:

    name = params[:name]
    users = User.where(User.arel_table[:name].matches("#{params[:name]}%")).to_a
    count = users.length
    
    name_without_collision = if count > 0
      loop do
        break "#{name}#{count}" unless users.any? { |u| u.name == "#{name}#{count}" }
        count += 1
      end
    else
      name
    end
    

    Since you’re calling to_a on the ActiveRecord::Relation, you don’t have to worry about accidentally sending multiple queries to the database, since it is being converted to a standard Ruby array.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Try this code - import java.io.StringReader; public class StringReaderTest { public static void main(String[]
Try this piece of code - public class WhitespaceTest { public static void main(String[]
try: html = urlopen('http://glbse.com/api/asset/' + asset.name) except: print 'error while updating the price of
try: recursive_function() except RuntimeError e: # is this a max. recursion depth exceeded exception?
Is this how you would pass a value (username in example below) to a
This is my first project in PHP based on the Oops Concept. I am
If I try to open this simple file in my Browser: <?php require_once 'classes/settings.php';
Basically I created this script that check if a file exists and then creates
when i try to construct a query to my db in my model like
I am getting error Wrong username or password when I try to start Eclipse.

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.