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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:39:48+00:00 2026-06-18T14:39:48+00:00

in my invite.rb model I have the following: class Invite < ActiveRecord::Base before_create :assign_code

  • 0

in my invite.rb model I have the following:

class Invite < ActiveRecord::Base
  before_create :assign_code
  validates_uniqueness_of :code, :case_sensitive => false, :message => "This invite code is taken!"

  protected

  # Create a safe random token and ensuring it's uniqueness for the model
  def assign_code
    begin
      code = SecureRandom.urlsafe_base64
    end while Invite.where(:code => code).exists?
    self.code = code
  end

Problem is in the logs I see the follow below. Why is rails doing a query for a code which is null, seems like a wasted query.

Invite Exists (0.5ms)  SELECT 1 AS one FROM "invites" WHERE "invites"."code" IS NULL LIMIT 1
Invite Exists (0.3ms)  SELECT 1 AS one FROM "invites" WHERE "invites"."code" = 'mTGCX0yCyTgplGfGQ5hGeA' LIMIT 1

Any ideas? Thanks

  • 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-18T14:39:50+00:00Added an answer on June 18, 2026 at 2:39 pm

    You probably want to use before_validation :assign_code instead, that way the code gets set before it’s validated.

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

Sidebar

Related Questions

I have a model that looks like this: class Invite(models.Model): user = models.ForeignKey(User) event
I have some code written like so: class Invite(models.Model): STATE_UNKNOWN = 0 STATE_WILL_PLAY =
I have the following code: Index.cshtml: @using System.Web.Script.Serialization @model MvcApplication3.ViewModels.PersonViewModel <script src=../../Scripts/knockout-2.1.0.js type=text/javascript></script> <!--
I have a model that looks like this class RSVP (models.Model): def __unicode__(self): return
I have created an invite friends code, which is properly working if an FB
This is a snippet of a user invite code. The code should get the
i have a model using m2m feature: class Classroom(models.Model): user = models.ForeignKey(User, related_name =
This code just keeps giving me errors. I have a field (datetime) in my
I'm trying to follow the code from the django docs: class Person(models.Model): name =
Say I have the good old person/group/membership relationship class Person(models.Model): name = models.CharField(max_length=128) def

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.