6.2 the uniqueness caveat when saving into database: adding index to tables!!!
- 博客分类:
- ruby and rails
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")
)
发表评论
-
12.3.3 scaling issue of the status feed
2011-10-30 17:54 816the problem of the implementati ... -
12.3 the status feed
2011-10-30 15:34 8521. we need to get all the micro ... -
12.2 a working follow button with Ajax
2011-10-29 18:10 9101. in the last chapter, in the ... -
12.2 a web interface for following and followers.
2011-10-28 22:14 8761.before we do the UI, we need ... -
12. following user, 12.1 relationship model
2011-10-18 14:29 7471. we need to use a relationshi ... -
11.3 manipulating microposts.
2011-10-17 15:31 8931. since all micropost actions ... -
11.2 show microposts.
2011-10-17 12:01 6981. add test to test the new use ... -
11.1 user micropost -- a micropost model.
2011-10-17 10:43 11031. we will first generate a mic ... -
10.4 destroying users.
2011-10-16 15:47 733in this chapter, we will add de ... -
10.3 showing users list
2011-10-15 20:41 768in this chapter, we will do use ... -
10.2 protect pages.
2011-10-15 15:11 657again, we will start from TD ... -
10.1 updating users.
2011-10-14 18:30 7051. git checkout -b updating-use ... -
9.4 sign out
2011-10-13 15:21 732whew!!!, last chapter is a long ... -
9.3 sign in success.
2011-10-12 15:39 7411. we will first finish the cre ... -
9.1 about flash.now[:error] vs flash[:error]
2011-10-12 15:37 722There’s a subtle difference ... -
9.2 sign in failure
2011-10-12 12:19 655start from TDD!!! 1. requir ... -
9.1 sessions
2011-10-12 10:00 643a session is a semi-permanent c ... -
what test framework should you use?
2011-10-11 16:56 0for integration test, i have no ... -
what test framework should you use?
2011-10-11 16:56 0<p>for integration test, ... -
8.4 rspec integration tests
2011-10-11 16:53 714in integration test, you can te ...
相关推荐
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 <| x|] where 0; (the Dirichlet condition ...
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 ...
- 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 ...
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 ...
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...
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. ...
无限时滞的中立型随机微分方程解的存在性与唯一性,莫迟,罗交晚,在本文中我们考虑无限时滞的中立型随机微分方程。在非LIPSCHITZ条件下得到了解的存在性与唯一性 ,并且将LIPSCHITZ条件下的情况作为一�
《Salient Region Detection by UFO: Uniqueness, Focusness and Objectness源代码》是2013年在ICCV(International Conference on Computer Vision)上发表的一篇关于显著性检测的学术论文的源代码。这篇论文引入了...
- 文档中提到:“Enjoy that uniqueness... You don’t have to pretend in order to seem more like someone else.” 这句话强调了每个人的独特性是值得珍视的,不应为了迎合他人而改变自己。 2. **个人价值的...
三维相场Navier-Stokes泡囊流体相互作用模型弱解的唯一性,赵继红,刘桥,本文研究了一类刻画泡囊薄膜在不可压粘性流体中形变的水动力学系统。在三维情形下,我们建立了此系统弱解的一些唯一性准则,这些准�
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 ...
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), ...
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 ...
### 高斯核在尺度空间滤波中的唯一性 #### 引言 在信号处理领域,尺度空间滤波(Scale-Space Filtering)是一种构建信号多层次符号描述的技术,它通过将原始信号转换为一系列不同尺度版本来实现。...
3. 浏览器指纹的唯一性(Uniqueness of Browser Fingerprint):研究表明现代浏览器的指纹包含了足够多的唯一信息,以至于即使随机选择一个浏览器,也很少有其他浏览器会与其具有相同的指纹。这增加了通过浏览器指纹...
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 ...
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 ...