`
peryt
  • 浏览: 55667 次
  • 来自: ...
最近访客 更多访客>>
社区版块
存档分类
最新评论
  • waiting: 既然都指定了dataType为'script'那就不必特别在b ...
    jQuery

6.2 the uniqueness caveat when saving into database: adding index to tables!!!

阅读更多

using validates :email, :uniqueness => true 

 

doesn't guarantee uniqueness!!!!!

 

here is why:

 

1. Alice sign up with address alice@wonderland.com

2. Alice accidentally clicks on "Submit" twice, sending two requests in quick succession.

3. the following sequence occurs:  request 1 creat a user in memory that passes validation, request 2 does the same, request 1's user gets saved, request 2's user gets saved.

4. two records with same email address in database.

 

this will probably happen in a scaling server.

 

how to solve it????

 

easy, you just need to enforce uniqueness at the database level!!!


our method is to creste database index on the eamil column, and then require that the index be unique!!

 

so we need to update our data model using a new migration, 

 

rails generate migration add_email_uniqueness_index

 

this will creste an empty file, we need to fill in it:

 

 

class AddEmailUniquenessIndex < ActiveRecord::Migration

  def self.up
    add_index :users, :email, :unique => true 
  end

  def self.down
    remove_index :users, :email
  end

end

 

then rake db:migrate

 

(adding index not only solve the uniqueness problem, it also solve another efficiency problem:

when searching by email column


find_by_email("abcd@abcd.com")

 

)

 

0
0
分享到:
评论

相关推荐

    Uniqueness of the Positive Radial Solution on an Annulus

    The purpose of this note is to prove uniqueness of the positive radial solution to the Dirichlet problem for 2u& u+ u3=0, (1.1) on the annulus 0= [x # R3 : R1 &lt;| x|] where 0; (the Dirichlet condition ...

    微软内部资料-SQL性能优化5

    If you build a clustered index without specifying the unique keyword, SQL Server forces uniqueness by adding a uniqueifier to the rows when necessary. This uniqueifier is a 4-byte value added as an ...

    FlexGraphics_V_1.79_D4-XE10.2_Downloadly.ir

    - FIX: The TFlexPanel.FindControlAtPoint method maked virtual to realize RealTime-capability when on mouse cursor moving the flex-object search not occurs. - FIX: After deleting the selected points ...

    Collections

    Sets are useful when the order of elements is not important but uniqueness is critical. ##### Set Fundamentals Sets can be mutable or immutable. They are initialized either by providing an initial ...

    Google C++ Style Guide(Google C++编程规范)高清PDF

    To guarantee uniqueness, they should be based on the full path in a project's source tree. For example, the file foo/src/bar/baz.h in project foo should have the following guard: #ifndef FOO_BAR_BAZ...

    UNIQUENESS OF POSITIVE RADIAL SOLUTIONS

    We study the uniqueness of positive radial solutions to the Dirichlet boundary value problem for the semilinear elliptic equation u + f( | x | , u) = 0 in a finite ball or annulus in R n, n 3. ...

    Existence and Uniqueness of the Solutions to Neutral Stochastic Differential Equations with Infinite Delay

    无限时滞的中立型随机微分方程解的存在性与唯一性,莫迟,罗交晚,在本文中我们考虑无限时滞的中立型随机微分方程。在非LIPSCHITZ条件下得到了解的存在性与唯一性 ,并且将LIPSCHITZ条件下的情况作为一�

    Salient Region Detection by UFO: Uniqueness, Focusness and Objectness源代码

    《Salient Region Detection by UFO: Uniqueness, Focusness and Objectness源代码》是2013年在ICCV(International Conference on Computer Vision)上发表的一篇关于显著性检测的学术论文的源代码。这篇论文引入了...

    2021精品手抄报系列-英语小报 (47).docx

    - 文档中提到:“Enjoy that uniqueness... You don’t have to pretend in order to seem more like someone else.” 这句话强调了每个人的独特性是值得珍视的,不应为了迎合他人而改变自己。 2. **个人价值的...

    On the Uniqueness of Weak Solutions for the 3D Phase Field Navier-Stokes Vesicle-Fluid Interaction Model

    三维相场Navier-Stokes泡囊流体相互作用模型弱解的唯一性,赵继红,刘桥,本文研究了一类刻画泡囊薄膜在不可压粘性流体中形变的水动力学系统。在三维情形下,我们建立了此系统弱解的一些唯一性准则,这些准�

    On the Comparison and Uniqueness for Solutions of BSDEs with Continuous Coefficients 

    On the Comparison and Uniqueness for Solutions of BSDEs with Continuous Coefficients ,徐玉红,,As we known that there may be more than one solution for backward stochastic differential equation ...

    Redis-Essentials.pdf

    Uniqueness support is added to the String and Hash implementations by using the Sorted Set and HyperLogLog data types, respectively. Preface [ viii ] Chapter 4, Commands (Where the Wild Things Are), ...

    minimal_perl_for_unix_and_linux_people.pdf

    of his uniqueness and importance to the Perl community. An auspicious weather non-event During one week while Damian was in Seattle as a visiting instructor for Consultix, we took an extended bike ...

    Uniqueness of the Gaussian Kernel for Scale-Space Filtering

    ### 高斯核在尺度空间滤波中的唯一性 #### 引言 在信号处理领域,尺度空间滤波(Scale-Space Filtering)是一种构建信号多层次符号描述的技术,它通过将原始信号转换为一系列不同尺度版本来实现。...

    browser-uniqueness.pdf2

    3. 浏览器指纹的唯一性(Uniqueness of Browser Fingerprint):研究表明现代浏览器的指纹包含了足够多的唯一信息,以至于即使随机选择一个浏览器,也很少有其他浏览器会与其具有相同的指纹。这增加了通过浏览器指纹...

    Java.for.Bioinformatics.and.Biomedical.Applications.(2007).part1.rar

    Instead the uniqueness of this book is that after just a short introduction, it takes a deep dive into demonstrating how to build highly functional graphical user interfaces for common and widely ...

    Advanced Engineering Mathematics

    6.8 Reduction to the Standard Form u + f(x)u = 0 324 6.9 Systems of Ordinary Differential Equations: An Introduction 326 6.10 A Matrix Approach to Linear Systems of Differential Equations 333 6.11 ...

Global site tag (gtag.js) - Google Analytics