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

  • Home
  • SEARCH
  • 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 6944491
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:18:49+00:00 2026-05-27T13:18:49+00:00

I am working on a simple rails project and i would like to know

  • 0

I am working on a simple rails project and i would like to know if it is possible for me to find an object with two different parameters like

def show
@user = User.find_by_name(params[:name])
or 
@user = User.find_by_id(params[:id])
end 

In this case i want to be able to find users either by their id or by their name so that when i type the url like localhost:3000/users/mike it takes me to the users showpage of when i type
localhost:3000/users/4 it also takes me to the same users show page. Please how can this done if it is possible in rails.

  • 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-27T13:18:50+00:00Added an answer on May 27, 2026 at 1:18 pm

    I would probably define a special find method in your User model that could look something like this:

    class User < ActiveRecord::Base
    
      def self.find_by_id_or_name(arg)
        # Checks to see if the supplied argument is numerical i.e. an id, not name
        if arg.match(/^[0-9]+$/)
          # send argument to default find method that looks up by id
          User.find(arg)
        else
          # send argument to find_by_name! to look up record by name instead.
          # The added ! makes sure that if no record is found, an RecordNotFound error is raised
          User.find_by_name!(arg)
        end
      end
    end
    

    Then you could use this method in your controller like this:

    def show
      @user = User.find_by_id_or_name(params[:id])
    end
    

    Edit:

    In addition, for this to be failsafe, you should probably make sure that a user name can not consist of only numbers so that it does not use find by the id parameter when it has really been given a user name. But that can be handled by validations.

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

Sidebar

Related Questions

Im working on a simple rails project were users have an attribute called is_admin,
I'm working on writing simple unit tests for a Rails 3 project, but I'm
I'm working on a ruby on rails project that requires recording some simple user
I'm working on a simple rails project where I can create a post. My
I am working on a rails project. Using the tag observe_field, I am taking
I am working on a simple Rails/jQuery HTML templater app which stores a series
I am trying to get a simple demo of Ruby on Rails working on
I'm working on my personal rails project and I'm still learning a lot of
I am working on a a rails project based on social media networks. Is
I'm trying to get Blueprint (SCSS-Syntax) working with my Rails (3.1) project. To keep

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.