Rails

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

Posted by Weston Ganger on October 25, 2016

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:


Posted in Rails and Tagged with devise rails 


Want me to help develop your next project or application?

Contact Us

Specializing in Ruby-on-Rails, Javascript, Multi-Platform Electron Desktop Apps and Hybrid Cordova Mobile Apps.

Recommended Posts