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.
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!
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
Regexpin theMatchablemodule. And since youinclude Mongoid::Documentin your models this class is getting invoked as apposed to the ruby's RegexpThe solution would be to change the namespace in you code.
You could do something along these lines.
Hope that helps ;)
Srinidhi P