Skip to content

Instantly share code, notes, and snippets.

@walshyb
Last active August 17, 2020 17:45
Show Gist options
  • Save walshyb/c72538f9d755823c1954820f0467da16 to your computer and use it in GitHub Desktop.
Save walshyb/c72538f9d755823c1954820f0467da16 to your computer and use it in GitHub Desktop.
Devise method overrides in User model
# app/models/user.rb
class User
has_many :email_addresses
accepts_nested_attributes_for :email_addresses
# ... association definitions and other methods
# Define our overrides:
def email_required?
false
end
def will_save_change_to_email?
false
end
def self.find_first_by_auth_conditions(warden_conditions)
conditions = warden_conditions.dup
# If "email" is an attribute in the conditions,
# remove it and save to variable
if email = conditions.delete(:email)
# Search through users by condition and also by
# users who have associations to the provided email
where(conditions.to_h).includes(:email_addresses).where(email_addresses: { email: email }).first
else
# If "email" is not an attribute in the conditions,
# just search for users by the conditions as normal
where(conditions.to_h).first
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment