我有一个带有这些模型的 Rails 4.2、Mongoid 4 项目:
class Customer #aka Company
include Mongoid::Document
has_many :branches
end
class Branch
include Mongoid::Document
field :name, type: String, default: ""
belongs_to :customer
end
我想找到所有拥有名为“纽约”的分支机构的客户(又名公司)。我认为这段代码会起作用:
branches = Branch.where(name: "New York").map(&:_id)
=> [BSON::ObjectId('54f76cef6272790316390100')]
Customer.where(:branch_ids => branches).entries
但是,无论我尝试什么,它总是返回一个空数组。代替branch_ids
, 我也尝试过branches
, branch
, branches_id
, 和其他的,但无济于事。我也尝试将转换BSON::ObjectID
为普通string
,但这也不起作用。
那么,基本上,我如何根据关联 ID 数组搜索模型?谢谢。