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 


Need help on your next project or application?

I specialize in Ruby-on-Rails, AngularJS, Javascript, Bootstrap, and Hybrid Mobile Apps with Cordova & Ionic.

Contact Me

Recommended Posts