I'm using mongoid, and have the following code:
class Users::User
include Mongoid::Document
field :username, type: String
has_many :emails, class_name: "Users::Email"
end
class Users::Email
include Mongoid::Document
field :email, type: String
belongs_to :user, class_name: "Users::User", inverse_of: :emails
end
database:
#users collection
{
"_id" : ObjectId("5162de8a359f10cbf700000c"),
"username" : "bilbo"
}
#emails collection
{
"_id" : ObjectId("5162de8a359f10cbf700000b"),
"email" : "bilbo@jenkins.com",
"user_id" : ObjectId("5162de8a359f10cbf700000c"),
}
I'm trying to find with the following query:
Users::User.includes(:emails).any_of({username: login},{"emails.email"=> login}).first
and I don't know why, but this query ignoring search in emails relation.
When login = "bilbo"
=> true, but when login = "bilbo@jenkins.com"
=> nil
So, what I'm doing wrong?