2

我正在使用 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

那么,我做错了什么?

4

1 回答 1

3

你需要一个连接来做你想做的事情,而 Mongoid 没有连接。如果您只需要通过用户访问电子邮件,您可以对它们进行非规范化并将它们嵌入到Users::User.

class Users::User
  include Mongoid::Document
  field :username, type: String
  embeds_many :emails, class_name: "Users::Email"
end

class Users::Email
  include Mongoid::Document

  field :email, type: String

  embedded_in :user, class_name: "Users::User", inverse_of: :emails
end

这样,您可以查询用户的电子邮件:

irb(main):011:0> login = "bilbo@jenkins.com"
=> "bilbo@jenkins.com"
irb(main):012:0> Users::User.any_of({username: login},{"emails.email"=> login}).first
=> #<Users::User _id: 5163ee96e44f7b0301000001, username: "bilbo">

如果Users::Email的唯一属性是email您可以更进一步,完全省略模型并将字符串存储在数组中:

class Users::User
  include Mongoid::Document
  field :username, type: String
  field :emails, type: Array
end

查询变得更加容易:

Users::User.any_of({username: login},{"emails"=> login}).first
=> #<Users::User _id: 5163ef95e44f7b6254000001, username: "bilbo", emails: ["bilbo@jenkins.com"]>
于 2013-04-09T10:42:17.727 回答