我必须建模,Library
并且Book
。
class Library
include Mongoid::Document
field :name, type: String
has_many :books
scope :first_scope, -> do
logger.debug Book.all.entries
where(name: "Foo")
end
scope :second_scope, -> do
logger.debug Book.all.entries
where(name: "Bar")
end
end
class Book
include Mongoid::Document
field :title, type: String
belongs_to :library
end
现在,如果我像这样链接作用域Library.first_scope.second_scope
,则第一个调试将返回现有书的列表,这是我期望的,但是第二个调试将返回我现有库的列表,就像我所做的那样logger.debug Library.all.entries
。
这是为什么?为什么我可以Book
在第一个作用域中访问集合,而不能在第二个作用域中访问呢?Book
在第二个作用域中,我如何访问该集合?
我最终使用了Moped,它似乎可以正常工作……不知道为什么。
scope :second_scope, -> do
logger.debug Book.collection.find.first # Returns a Book, yay!
where(name: "Bar")
end
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句