NoamB/sorcery — Issue #90: find_by_username and find_by_email don't work with Mongoid
Author: theycallmeswift
Created: 2011-10-13 03:48:35+00:00
State: open

Background:
Rails 3.1.0
Mongoid 2.3.0
Sorcery 0.7.0

When searching for User's using the find_by_username or find_by_email, the data that gets passed into the `User.where(...)` method is malformed.

>> User.find_by_username("swift")

41
42 def find_by_username(username)
43 query = sorcery_config.username_attribute_names.map {|name| {name => username}}
44 debugger
=> 45 where(query).first
46 end
47

(rdb:1) query
[{:username=>"swift"}]

When query gets passed into `where(query)` it results in the following error:

NoMethodError: undefined method `expand_complex_criteria' for [{:username=>"swift"}]:Array

This happens because `expand_complex_criteria` takes a hash, not an array. The simple fix is to do something like the following:

def find_by_username(username)
query = sorcery_config.username_attribute_names.map {|name| {name => username}}
where(query.first).first
end