Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ActiveRecord::RecordInvalid: Validation failed: Email has already been taken #900

Closed
aaronskiba opened this issue Sep 12, 2024 · 1 comment · Fixed by #907
Closed

ActiveRecord::RecordInvalid: Validation failed: Email has already been taken #900

aaronskiba opened this issue Sep 12, 2024 · 1 comment · Fixed by #907
Assignees

Comments

@aaronskiba
Copy link
Collaborator

View details in Rollbar: https://app.rollbar.com/a/ualbertalib/fix/item/dmp_assistant/534


ActiveRecord::RecordInvalid: Validation failed: Email has already been taken
  File "/var/www/sites/dmp/vendor/ruby/3.1.0/gems/activerecord-6.1.7.8/lib/active_record/validations.rb", line 80, in raise_validation_error
  File "/var/www/sites/dmp/vendor/ruby/3.1.0/gems/activerecord-6.1.7.8/lib/active_record/validations.rb", line 53, in save!
  File "/var/www/sites/dmp/vendor/ruby/3.1.0/gems/activerecord-6.1.7.8/lib/active_record/transactions.rb", line 302, in block in save!
  File "/var/www/sites/dmp/vendor/ruby/3.1.0/gems/activerecord-6.1.7.8/lib/active_record/transactions.rb", line 354, in block in with_transaction_returning_status
  File "/var/www/sites/dmp/vendor/ruby/3.1.0/gems/activerecord-6.1.7.8/lib/active_record/connection_adapters/abstract/database_statements.rb", line 320, in block in transaction
  File "/var/www/sites/dmp/vendor/ruby/3.1.0/gems/activerecord-6.1.7.8/lib/active_record/connection_adapters/abstract/transaction.rb", line 319, in block in within_new_transaction
  File "/var/www/sites/dmp/vendor/ruby/3.1.0/gems/activesupport-6.1.7.8/lib/active_support/concurrency/load_interlock_aware_monitor.rb", line 26, in block (2 levels) in synchronize
  File "/var/www/sites/dmp/vendor/ruby/3.1.0/gems/activesupport-6.1.7.8/lib/active_support/concurrency/load_interlock_aware_monitor.rb", line 25, in handle_interrupt
  File "/var/www/sites/dmp/vendor/ruby/3.1.0/gems/activesupport-6.1.7.8/lib/active_support/concurrency/load_interlock_aware_monitor.rb", line 25, in block in synchronize
  File "/var/www/sites/dmp/vendor/ruby/3.1.0/gems/activesupport-6.1.7.8/lib/active_support/concurrency/load_interlock_aware_monitor.rb", line 21, in handle_interrupt
  File "/var/www/sites/dmp/vendor/ruby/3.1.0/gems/activesupport-6.1.7.8/lib/active_support/concurrency/load_interlock_aware_monitor.rb", line 21, in synchronize
  File "/var/www/sites/dmp/vendor/ruby/3.1.0/gems/activerecord-6.1.7.8/lib/active_record/connection_adapters/abstract/transaction.rb", line 317, in within_new_transaction
  File "/var/www/sites/dmp/vendor/ruby/3.1.0/gems/activerecord-6.1.7.8/lib/active_record/connection_adapters/abstract/database_statements.rb", line 320, in transaction
  File "/var/www/sites/dmp/vendor/ruby/3.1.0/gems/activerecord-6.1.7.8/lib/active_record/transactions.rb", line 350, in with_transaction_returning_status
  File "/var/www/sites/dmp/vendor/ruby/3.1.0/gems/activerecord-6.1.7.8/lib/active_record/transactions.rb", line 302, in save!
  File "/var/www/sites/dmp/vendor/ruby/3.1.0/gems/activerecord-6.1.7.8/lib/active_record/suppressor.rb", line 48, in save!
  File "/var/www/sites/dmp/vendor/ruby/3.1.0/gems/activerecord-6.1.7.8/lib/active_record/persistence.rb", line 55, in create!
  File "/var/www/sites/dmp/app/models/user.rb", line 193, in create_from_provider_data
  File "/var/www/sites/dmp/app/controllers/users/omniauth_callbacks_controller.rb", line 29, in openid_connect
  File "/var/www/sites/dmp/vendor/ruby/3.1.0/gems/actionpack-6.1.7.8/lib/action_controller/metal/basic_implicit_render.rb", line 6, in send_action
  File "/var/www/sites/dmp/vendor/ruby/3.1.0/gems/actionpack-6.1.7.8/lib/abstract_controller/base.rb", line 228, in process_action
  File "/var/www/sites/dmp/vendor/ruby/3.1.0/gems/actionpack-6.1.7.8/lib/action_controller/metal/rendering.rb", line 30, in process_action
  File "/var/www/sites/dmp/vendor/ruby/3.1.0/gems/actionpack-6.1.7.8/lib/abstract_controller/callbacks.rb", line 42, in block in process_action
  File "/var/www/sites/dmp/vendor/ruby/3.1.0/gems/activesupport-6.1.7.8/lib/active_support/callbacks.rb", line 117, in block in run_callbacks
  File "/var/www/sites/dmp/vendor/ruby/3.1.0/gems/turbo-rails-2.0.5/lib/turbo-rails.rb", line 24, in with_request_id
  File "/var/www/sites/dmp/vendor/ruby/3.1.0/gems/turbo-rails-2.0.5/app/controllers/concerns/turbo/request_id_tracking.rb", line 10, in turbo_tracking_request_id
  File "/var/www/sites/dmp/vendor/ruby/3.1.0/gems/activesupport-6.1.7.8/lib/active_support/callbacks.rb", line 126, in block in run_callbacks
  File "/var/www/sites/dmp/vendor/ruby/3.1.0/gems/actiontext-6.1.7.8/lib/action_text/rendering.rb", line 20, in with_renderer
  File "/var/www/sites/dmp/vendor/ruby/3.1.0/gems/actiontext-6.1.7.8/lib/action_text/engine.rb", line 59, in block (4 levels) in <class:Engine>
  File "/var/www/sites/dmp/vendor/ruby/3.1.0/gems/activesupport-6.1.7.8/lib/active_support/callbacks.rb", line 126, in instance_exec
  File "/var/www/sites/dmp/vendor/ruby/3.1.0/gems/activesupport-6.1.7.8/lib/active_support/callbacks.rb", line 126, in block in run_callbacks
  File "/var/www/sites/dmp/vendor/ruby/3.1.0/gems/activesupport-6.1.7.8/lib/active_support/callbacks.rb", line 137, in run_callbacks
  File "/var/www/sites/dmp/vendor/ruby/3.1.0/gems/actionpack-6.1.7.8/lib/abstract_controller/callbacks.rb", line 41, in process_action
  File "/var/www/sites/dmp/vendor/ruby/3.1.0/gems/actionpack-6.1.7.8/lib/action_controller/metal/rescue.rb", line 22, in process_action
  File "/var/www/sites/dmp/vendor/ruby/3.1.0/gems/actionpack-6.1.7.8/lib/action_controller/metal/instrumentation.rb", line 34, in block in process_action
  File "/var/www/sites/dmp/vendor/ruby/3.1.0/gems/activesupport-6.1.7.8/lib/active_support/notifications.rb", line 203, in block in instrument
  File "/var/www/sites/dmp/vendor/ruby/3.1.0/gems/activesupport-6.1.7.8/lib/active_support/notifications/instrumenter.rb", line 24, in instrument
  File "/var/www/sites/dmp/vendor/ruby/3.1.0/gems/activesupport-6.1.7.8/lib/active_support/notifications.rb", line 203, in instrument
  File "/var/www/sites/dmp/vendor/ruby/3.1.0/gems/actionpack-6.1.7.8/lib/action_controller/metal/instrumentation.rb", line 33, in process_action
  File "/var/www/sites/dmp/vendor/ruby/3.1.0/gems/actionpack-6.1.7.8/lib/action_controller/metal/params_wrapper.rb", line 249, in process_action
  File "/var/www/sites/dmp/vendor/ruby/3.1.0/gems/activerecord-6.1.7.8/lib/active_record/railties/controller_runtime.rb", line 27, in process_action
  File "/var/www/sites/dmp/vendor/ruby/3.1.0/gems/actionpack-6.1.7.8/lib/abstract_controller/base.rb", line 165, in process
  File "/var/www/sites/dmp/vendor/ruby/3.1.0/gems/actionview-6.1.7.8/lib/action_view/rendering.rb", line 39, in process
  File "/var/www/sites/dmp/vendor/ruby/3.1.0/gems/actionpack-6.1.7.8/lib/action_controller/metal.rb", line 190, in dispatch
  File "/var/www/sites/dmp/vendor/ruby/3.1.0/gems/actionpack-6.1.7.8/lib/action_controller/metal.rb", line 254, in dispatch
  File "/var/www/sites/dmp/vendor/ruby/3.1.0/gems/actionpack-6.1.7.8/lib/action_dispatch/routing/route_set.rb", line 50, in dispatch
  File "/var/www/sites/dmp/vendor/ruby/3.1.0/gems/actionpack-6.1.7.8/lib/action_dispatch/routing/route_set.rb", line 33, in serve
  File "/var/www/sites/dmp/vendor/ruby/3.1.0/gems/actionpack-6.1.7.8/lib/action_dispa
aaronskiba added a commit that referenced this issue Sep 16, 2024
This change adds the PR idea from #902.
The change seeks to address the following issue: #900

Co-Authored-By: Omar Rodriguez Arenas <[email protected]>
@aaronskiba aaronskiba reopened this Oct 10, 2024
@aaronskiba
Copy link
Collaborator Author

aaronskiba commented Oct 10, 2024

3.1.4 :001 > User.find_by!(email: '[email protected]')
  User Load (3.9ms)  SELECT "users".* FROM "users" WHERE "users"."email" = $1 LIMIT $2  [["email", "[email protected]"], ["LIMIT", 1]]                       
 => #<User id: 26479, firstname: "test", surname: "test", email: "[email protected]", created_at: "2024-10-10 17:13:10.783440000 +0000", updated_at: "2024-10-10 17:13:10.783440000 +0000", other_organisation: nil, dmponline3: nil, accept_terms: true, org_id: 59, api_token: nil, language_id: 1, recovery_email: nil, active: true, department_id: nil, last_api_access: nil, prefs:

3.1.4 :002 > user = User.find_or_initialize_by(email: '[email protected]')
  User Load (3.8ms)  SELECT "users".* FROM "users" WHERE "users"."email" = $1 LIMIT $2  [["email", "[email protected]"], ["LIMIT", 1]]ise.friendly_token[0, 20])
  Language Load (2.2ms)  SELECT "languages".* FROM "languages" WHERE "languages"."default_language" = $1 ORDER BY "languages"."id" ASC LIMIT $2  [["default_language", true], ["LIMIT", 1]]

3.1.4 :003 > user.new_record?
 => true 

3.1.4 :004 > user.update!(firstname: 'test', surname: 'test', org: Org.find_by(is_other: true), accept_terms: true, password
: Devise.friendly_token[0, 20])
  Org Load (3.2ms)  SELECT "orgs".* FROM "orgs" WHERE "orgs"."is_other" = $1 LIMIT $2  [["is_other", true], ["LIMIT", 1]]
  TRANSACTION (2.9ms)  BEGIN
  User Exists? (2.1ms)  SELECT 1 AS one FROM "users" WHERE "users"."email" = $1 LIMIT $2  [["email", "[email protected]"], ["LIMIT", 1]]
  Language Load (1.0ms)  SELECT "languages".* FROM "languages" WHERE "languages"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
  TRANSACTION (2.7ms)  ROLLBACK
/usr/share/rvm/gems/ruby-3.1.4/gems/activerecord-6.1.7.8/lib/active_record/validations.rb:80:in `raise_validation_error': Validation failed: Email has already been taken (ActiveRecord::RecordInvalid)

The error seems to be caused due User.find_or_initialize_by() being case-sensitive when querying the email but user.update!() being case-insensitive (by applying .downcase) when saving the email.

aaronskiba added a commit that referenced this issue Oct 10, 2024
This commit is intended to resolve the following issue: #900
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant