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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:32:50+00:00 2026-05-28T03:32:50+00:00

In my Rails 3 app, I have a model called User with id and

  • 0

In my Rails 3 app, I have a model called User with id and username, both are indexed and unique. Somewhere in some views I have following:

<%= link_to 'Show this user', user %>

That creates a link like this:

<a href="/users/980190962">Show this user</a>

where that horrible number is the id (primary key). Now, for the SEFness of the site, I would like to use the username instead of the id wherever I use link_to “…”, user, but only for that User model. Adding parameters to the link_to does not help, because I already have it in many places (and I do not want to have to add another parameter everywhere all the time).

How can I override the behavior of the link_to helper only for one model?


Here is a complete answer to this question, as I deducted from the accepted answer. I added following to my User model:

def to_param  # overridden to use username as id, and not the id. More SEF.
  username
end
def self.find(id)
  @user = User.find_by_id(id)
  @user = User.find_by_username(id) if @user == nil
  throw ActiveRecord.RecordNotFound.new if @user == nil
  @user
end

This now makes all links generated with link_to use the username, while the find will try both the id and the username, making it also backward compatible.

Thought someone as new as me to Rails would find this useful.

  • 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-28T03:32:50+00:00Added an answer on May 28, 2026 at 3:32 am

    Not sure of what I’m saying, but overriding #to_param in your model may be what you’re looking for.

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

Sidebar

Related Questions

I'm creating a Rails app, and I have a model called User . In
I have a simple model setup in my Ruby on Rails app. (User {name,
I am developing a Ruby on Rails app and I have a model called
In my Rails app, I have a model called Photo and a PhotosController. As
I have a rails app (running on version 2.2.2) that has a model called
I have a photo rails app using paperclip. My model called photo and the
For a Rails 3.0 Todo app, I have a Tasks model with a Status
I have a Rails app with some basic models. The website displays data retrieved
I have a rails app where i have a Contact model and with an
I'm trying to make logging for my rails app and have some dilemmas over

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.