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

The Archive Base Latest Questions

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

Possible Duplicate: Ruby on Rails Callback, what is difference between :before_save and :before_create? I’m

  • 0

Possible Duplicate:
Ruby on Rails Callback, what is difference between :before_save and :before_create?

I’m looking to replace Ruby on Rails current id from /1, /2, /3, etc to a randomly generated URL, for example /csd8dsv68dvs.

After looking into creating it, I found this in the post model

before_create { self.code = SecureRandom.hex(5) }

What does the before_create do and how can I implement the random id?

Update

It seems I got one part wrong. I’m not looking to change the ID, I just want to change the URL to a random string that a user won’t be able to guess.

  • 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-15T10:20:54+00:00Added an answer on June 15, 2026 at 10:20 am

    A great gem to help you with alternatives to a resource :id as the identfier through a route is friendly_id. There is even a RailsCast on it.

    before_create is an ActiveRecord callback and is well explained both here on SO (as others have commented) and in the API.

    If you chose to use the raw

    before_create { self.code = SecureRandom.hex(5) }
    

    You could modify your controller action to find the resource using this code instead of the id.

    def some_action
      @resource = ModelClass.find_by_code!(params[:id])
    end
    

    You’d pass the :code attribute to routes for the resource in your views

    resource_path(@resource.code)
    

    where resource_path is the route method for your resource.

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

Sidebar

Related Questions

Possible Duplicate: Ruby: difference between || and ‘or’ Using Ruby || and or are
Possible Duplicate: Array slicing in Ruby: looking for explanation for illogical behaviour (taken from
Possible Duplicate: Difference between -%> and %> in rails I need to know what
Possible Duplicate: Ruby: difference between || and 'or' In ruby, isn't 'or' and '||'
Possible Duplicate: Ruby - What is the difference between map, each and collect? I
Possible Duplicate: Ruby on Rails scalability/performance? I'm aware from the twitter shortages that Ruby
Possible Duplicate: (Django) Trim whitespaces from charField In ruby on rails is really easy
Possible Duplicate: Ruby On Rails 3 and Webrick issue $ rails server /Users/Vineeth/.rvm/gems/ruby-1.9.3-p194/gems/ruby-debug-base19-0.11.25/lib/ruby-debug-base.rb:1:in `require':
Possible Duplicate: Running a command from Ruby displaying and capturing the output I have
Possible Duplicate: Can't convert String into integer in ruby/ruby-on-rails I'm running through a tutorial

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.