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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T13:04:01+00:00 2026-05-20T13:04:01+00:00

In my User model, I have: validates_uniqueness_of :fb_uid (I’m using facebook connect). However, at

  • 0

In my User model, I have:

validates_uniqueness_of :fb_uid (I’m using facebook connect).

However, at times, I’m getting duplicate rows upon user sign up. This is Very Bad.

The creation time of the two records is within 100ms. I haven’t been able to determine if it happens in two separate requests or not (heroku logging sucks and only goes back so far and it’s only happened twice).

Two things:

  • Sometimes the request takes some time, because I query FB API for name info, friends, and picture.
  • I’m using bigint to store fb_uid (backend is postgres).

I haven’t been able to replicate in dev.

Any ideas would be extremely appreciated.

The signin function

def self.create_from_cookie(fb_cookie, remote_ip = nil)
    return nil unless fb_cookie
    return nil unless fb_hash = authenticate_cookie(fb_cookie)
    uid = fb_hash["uid"].join.to_i

    #Make user and set data
    fb_user = FacebookUser.new
    fb_user.fb_uid = uid
    fb_user.fb_authorized = true
    fb_user.email_confirmed = true
    fb_user.creation_ip = remote_ip
    fb_name_data, fb_friends_data, fb_photo_data, fb_photo_ext = fb_user.query_data(fb_hash)
    return nil unless fb_name_data
    fb_user.set_name(fb_name_data)
    fb_user.set_photo(fb_photo_data, fb_photo_ext)

    #Save user and friends to the db
    return nil unless fb_user.save
    fb_user.set_friends(fb_friends_data)
    return fb_user
end
  • 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-20T13:04:02+00:00Added an answer on May 20, 2026 at 1:04 pm

    I’m not terribly familiar with facebook connect, but is it possible to get two of the same uuid if two separate users from two separate accounts post a request in very quick succession before either request has completed? (Otherwise known as a race condition) validates_uniqueness_of can still suffer from this sort of race condition, details can be found here:

    http://apidock.com/rails/ActiveModel/Validations/ClassMethods/validates_uniqueness_of

    Because this check is performed
    outside the database there is still a
    chance that duplicate values will be
    inserted in two parallel transactions.
    To guarantee against this you should
    create a unique index on the field.
    See add_index for more information.

    You can really make sure this will never happen by adding a database constraint. Add this to a database migration and then run it:

    add_index :user, :fb_uid, :unique => true
    

    Now a user would get an error instead of being able to complete the request, which is usually preferable to generating illegal data in your database which you have to debug and clean out manually.

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

Sidebar

Related Questions

I have this validation in my user model. validates_uniqueness_of :email, :case_sensitive => false, :message
In my user model, I have an attribute called nickname and validates as such:
For example, if I have a user model and I need to validate login
I have a model Foo which have a ForeignKey to the User model. Later,
So I have a User model that :has_many other models like Documents, Videos, Posts
I have a data model that includes common columns like addedBy, editedby (user), addedDate,
Suppose you have two models, User and City, joined by a third model CityPermission:
I have a User model, and use an acts_as_authentic (from authlogic) on it. My
I have a user model on which I check to make sure that the
I have a User model which has_many Documents. Each Document's title must be unique

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.