我一直在關注本教程,http://ruby.railstutorial.org/chapters/modeling-users?version=3.2#top,我覺得它很棒,但它提到了一些我不明白的唯一性屬性。 這也是迄今爲止畝用戶文件:Ruby教程和「唯一性」
class User < ActiveRecord::Base
#these attributes can be modified by the users
attr_accessible :name, :email;
#validation testing
validates :name, presence: true, length: { maximum: 50 }
#regular expression (there is an official one)
VALID_EMAIL_REGEX = /\A[\w+\-.][email protected][a-z\d\-.]+\.[a-z]+\z/i
#and add it..
validates :email, presence: true, format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
end
它說:
「使用只會驗證:唯一不保證唯一性
愛麗絲不小心點擊‘提交’兩次,送兩個請求快速繼續 發生以下情況:請求1在內存中創建一個通過驗證的用戶,請求2執行相同操作,請求1的用戶得到保存,請求2的用戶得到保存 結果:具有完全相同電子郵件地址的兩條用戶記錄,儘管uniquenes的驗證。「
我嘗試使用控制檯(和User.create方法)使用相同的電子郵件地址創建2個用戶,唯一性似乎工作正常,因爲只有第一個進入sqlite3。那麼,什麼會導致錯誤或唯一性失敗?
C-Ruby(MRI和YARV)不使用原生線程,而是使用綠色線程。像JRuby或Rubinius這樣的替代實現使用本地線程。但是,這兩種類型都可以在單一過程中觀察比賽情況。 – 2012-03-11 20:41:16