私のinvite.rbモデルには、次のものがあります。
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
問題はログにあります。以下を参照してください。Railsがnullのコードに対してクエリを実行するのはなぜですか、無駄なクエリのように見えます。
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
何か案は?ありがとう