我default_scope
的大多数模型都有current_user.company
. 我在我的注册页面上使用了 client_side_validations gem,这意味着没有租户集。
当唯一性验证器在@user.email
default_scope 上运行时,会阻止验证正常运行(因为company_id
它是 nil),因此看起来总是一个唯一的结果。
我的用户模型:
# user.rb
...
default_scope { where(company_id: Company.current_id) }
...
此查询在验证电子邮件时运行:
Started GET "/validators/uniqueness?case_sensitive=true& \
user%5Bemail%5D=me%2B40%40example.com&_=1423897339854" \
for 76.175.168.2 at 2015-02-14 07:02:30 +0000
User Exists (0.4ms) SELECT 1 AS one FROM "users" WHERE \
"users"."company_id" IS NULL AND "users"."email" = 'me@example.com' LIMIT 1
当我default_scope
从我的用户模型中删除时,我得到了正确的结果和正确的验证:
Started GET "/validators/uniqueness?case_sensitive=true& \
user%5Bemail%5D=me%2B40%40example.com&_=1423897339854" \
for 76.175.168.2 at 2015-02-14 07:02:30 +0000
User Exists (0.4ms) SELECT 1 AS one FROM "users" WHERE \
"users"."email" = 'me@example.com' LIMIT 1
default_scope
当这个 gem 运行电子邮件验证器时,我最实用的方法是什么?