How To Make Devise Use Custom Scopes Or Conditions When Querying Users

Posted by Weston Ganger

A while ago I added a PR to the paranoia gem to add the option without_default_scope. This allows you to use this on your User model and skip all of the hassles of default scopes. When using this option on your user model you will need to tell devise to use a different scope when authenticating the user.

Here I show a couple of use cases.


class User < ActiveRecord::Base
  def self.find_for_authentication
    ### Without deleted records
    where(email: warden_conditions[:email], deleted_at: nil).first

    ### or Scope to subdomain
    #where(email: warden_conditions[:email], subdomain: warden_conditions[:subdomain]).first

    ### or Default
    #where(email: warden_conditions[:email]).first
  end
end


Related External Links:

Article Topic:Software Development - Rails

Date:October 25, 2016

Recommended Posts