我正在使用 mongoid,并具有以下代码:
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
数据库:
#users collection
{
"_id" : ObjectId("5162de8a359f10cbf700000c"),
"username" : "bilbo"
}
#emails collection
{
"_id" : ObjectId("5162de8a359f10cbf700000b"),
"email" : "bilbo@jenkins.com",
"user_id" : ObjectId("5162de8a359f10cbf700000c"),
}
我正在尝试使用以下查询进行查找:
Users::User.includes(:emails).any_of({username: login},{"emails.email"=> login}).first
我不知道为什么,但是这个查询忽略了电子邮件关系中的搜索。当login = "bilbo"
=> true,但当login = "bilbo@jenkins.com"
=> nil
那么,我做错了什么?