我在 Rails 3 中有以下 Mongoid 模型的范围:
class Expert
include Mongoid::Document
referenced_in :category
scope :currently_available, lambda { |category, limit|
limit ||= 5
{
:where => {:category => category, :state => 'available'},
:limit => limit
}
}
category
这是引用模型的一个实例:
class Category
include Mongoid::Document
references_many :experts, :inverse_of => :category
当我将范围称为Expert.currently_available(Category.first, 5)
时,我得到了一个 Criteria 对象:
ruby-1.9.2-p136 :110 > Expert.currently_available(Category.first, 5)
=> #<Mongoid::Criteria
selector: {:category=>#<Category _id: 4d95ea8773fdea4c47000003, _type: nil, _id: BSON::ObjectId('4d95ea8773fdea4c47000003'), title: "Tax Advisors", price: 5.5>, :state=>"available"},
options: {:limit=>5},
class: Expert,
embedded: false>
问题是:如何在此条件内加载集合?当我这样做时.to_a
,它说:
Cannot serialize an object of class Category into BSON
类别本身在直接拾取时是有效的 BSON obj,但在范围内它无法呈现引用 obj。
提前致谢!