Devise: NoMethodError (undefined method `escape' for Mongoid::Matchable::Regexp:Class)

Created on 4 Apr 2017  路  2Comments  路  Source: heartcombo/devise

I'm using Mongoid with Devise follow this page:
https://github.com/plataformatec/devise/wiki/How-To:-Allow-users-to-sign-in-using-their-username-or-email-address
But now suddenly i got this message:

NoMethodError (undefined method `escape' for Mongoid::Matchable::Regexp:Class):
at line:

escape_login = Regexp.escape(login)

Any idea for this issue?
Update: I realized after I upgraded Rails to 5.0.2, this error appear.

Most helpful comment

Hi @thiensubs ,

This does not seem to be an issue with devise, but more so with Mongoid.

I see here that a PR was merged that is causing this issue https://github.com/mongodb/mongoid/commit/1997fad53de0d4b63ec4af94a931b7166cbf269c
which basically allows searching in embedded documents.

As you can see the commit adds a class called Regexp in the Matchable module. And since you include Mongoid::Document in your models this class is getting invoked as apposed to the ruby's Regexp

The solution would be to change the namespace in you code.

You could do something along these lines.

  def self.find_first_by_auth_conditions(warden_conditions)
    conditions = warden_conditions.dup
    if login = conditions.delete(:login)
      self.any_of({ :mobile_number =>  /^#{::Regexp.escape(login)}$/i }, { :email =>  /^#{::Regexp.escape(login)}$/i },{ :register_number =>  /^#{::Regexp.escape(login)}$/i }).first
    else
      super
    end
  end

Hope that helps ;)
Srinidhi P

All 2 comments

Hi @thiensubs ,

This does not seem to be an issue with devise, but more so with Mongoid.

I see here that a PR was merged that is causing this issue https://github.com/mongodb/mongoid/commit/1997fad53de0d4b63ec4af94a931b7166cbf269c
which basically allows searching in embedded documents.

As you can see the commit adds a class called Regexp in the Matchable module. And since you include Mongoid::Document in your models this class is getting invoked as apposed to the ruby's Regexp

The solution would be to change the namespace in you code.

You could do something along these lines.

  def self.find_first_by_auth_conditions(warden_conditions)
    conditions = warden_conditions.dup
    if login = conditions.delete(:login)
      self.any_of({ :mobile_number =>  /^#{::Regexp.escape(login)}$/i }, { :email =>  /^#{::Regexp.escape(login)}$/i },{ :register_number =>  /^#{::Regexp.escape(login)}$/i }).first
    else
      super
    end
  end

Hope that helps ;)
Srinidhi P

I'm closing this because it seems like it's not a Devise issue, and a possible workaround was provided.
Feel free to reopen if this still happens.
Thank you!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

emn178 picture emn178  路  4Comments

mvz picture mvz  路  3Comments

edipox picture edipox  路  4Comments

Gorchel picture Gorchel  路  3Comments

jmarchello picture jmarchello  路  4Comments