I have default_scope
on most of my models that scopes by current_user.company
. I'm using the client_side_validations gem on my sign up page which means there is no tenant set.
When the uniqueness validator runs on @user.email
the default_scope prevents the validation from running properly (because company_id
is nil) and therefore always looks like a unique result.
My user model:
# user.rb
...
default_scope { where(company_id: Company.current_id) }
...
This query is run when validating email:
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
When I remove the default_scope
from my user model I get the correct result and proper validation:
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
What's the most practical way for me to override the default_scope
when this gem runs the email validator?