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

The Archive Base Latest Questions

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

I have a User model witch i have override the to_param method. i made

  • 0

I have a User model witch i have override the to_param method.

i made it like this:

class User < ActiveRecord::Base

  def to_param
    if self.id < 100
      if self.id < 10
        s = "00#{self.id}"
      else
        s = "0#{self.id}"
      end
    else
      s = "#{self.id}"
    end
    return "usr-#{s}"
  end

end

And on my controller:

user = User.find(params[:id])
@title = user.name

the problem is… i can`t get any users…

if i change the to_param method to: return "#{s}-usr" it simple ignores the -usr and search for the id without taking -usr

i already tried an after_save filter, but i could`n change an atribute on there.

how can i create an unique id to an user based on his id ?

  • 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-27T02:49:25+00:00Added an answer on May 27, 2026 at 2:49 am

    Rails won’t do inverse transformation for you, you have to do it yourself. I think you can do it like that:

    class User < ActiveRecord::Base
    
      # Returns formatted user id:
      #
      #   1    => 'usr-001'
      #   11   => 'usr-011'
      #   111  => 'usr-111'
      #   1111 => 'usr-1111'      
      #
      def to_param
        "usr-%03d" % id
      end 
    
      def self.from_param(param)
        # ruby strips leading zeros for us (e.g., '012'.to_i returns 12)
        # but we need to remove 'usr-' prefix first
        find(param[4..-1].to_i)
      end 
    
    end
    

    and then use it in wherever you need it:

    user = User.from_param params[:id]
    

    In addition, have a look at to_param docs. Description and examples clearly state that this method is used for generating urls, but developer is the one responsible for finding corresponding record by this parameter.

    Regarding strings formatting you can find more info in docs for String#% method and Kernel#format method it uses.

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

Sidebar

Related Questions

i have this model on mvc: public class User { public string Name {
How does the Model work? I have a Registry Class like this: public class
So I have a User model that :has_many other models like Documents, Videos, Posts
For instance, let's say I have a User model. Users have things like logins,
I have a model as follows: public class User : System.Web.Security.MembershipUser { public int
I have a model with a property defined like this: [Required(ErrorMessage=Please enter how many
Imagine a code like this one: @RequestMapping(value=/users, method=RequestMethod.GET) public String list(Model model) { ...
I have a model that has an overriden to_param method returning the (unique) name
This is the model: class Car(models.Model): user = models.ForeignKey(User, related_name='cars') name = models.CharField(max_length=64) Url
I have a model Model with a m2m field : user = .. fk

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.