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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T19:50:26+00:00 2026-06-04T19:50:26+00:00

I have the following code in my user model: before_save :create_remember_token private def create_remember_token

  • 0

I have the following code in my user model:

before_save :create_remember_token

  private
    def create_remember_token
      generate_token(:remember_token)
    end

    def generate_token(column)
      begin
        self[column] = SecureRandom.urlsafe_base64
      end while User.exists?(column => self[column])
    end

This works well, but the trouble is that I was expecting it to fail. I was under the impression that the line: while User.exists?(column=>self[column]) would return false, since the record has not yet been saved to the db, and thus, the remember_token would not be set.

But newly created users do have their remember_token set, and I’m a little bit confused as to how this can be possible. Am I missing something?

  • 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-04T19:50:28+00:00Added an answer on June 4, 2026 at 7:50 pm

    In case anyone else is wondering, the code inside a begin end expression will run once before the condition is evaluated, unless you provide a rescue clause:

    # x is not incremented as condition is evaluated first
    
    1.9.3p194 :033 > x=0
     => 0
    1.9.3p194 :034 > x+=1 while x>14
     => nil 
    1.9.3p194 :035 > x
     => 0 
    
    # x is incremented because code inside begin end is evaluated once before the condition 
    1.9.3p194 :036 > begin
    1.9.3p194 :037 >     x+=1
    1.9.3p194 :038?>   end while x>14
     => nil 
    1.9.3p194 :039 > x
     => 1 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following code class User attr_accessor :name end u = User.new u.name =
Say I have a User model with the following code in User.rb: before_create :create_dependencies
If use MongoRepository, You can have following code: @Repository public interface UserRepo extends MongoRepository<User,
I have the following code that loads a user's Active Directory DirectoryEntry object from
I have the following code which lets the user plot two points on a
I have the following code that prompts the user to select a range of
I have the following code in my controller action method: if (User.Identity.IsAuthenticated) { //
If we have the following code, then when user clicks an Edit button, page
I have the following code: if($result = $this->Email->peopleFollowingEmail($follow['User']['email'], $viewer['User']['fname'].' '.$viewer['User']['lname'], $viewer['User']['username'])) { $pf_model->id =
I have the following code in settings.py #EMAIL_USE_TLS = True EMAIL_HOST = 'smtp.gmail.com' EMAIL_HOST_USER

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.