我有一个使用 rabl 和 mongoid 3.1.0 的简单应用程序,执行以下操作:
# index
@products = current_shop.products
rabl 代码如下所示:
# index.json.rabl
collection @products
extends 'api/products/show'
# show.json.rabl
object @product
attributes :id
当我点击它时,日志显示与@products 相关的 4 个请求:
MOPED: 127.0.0.1:27017 QUERY database=bamboo_development collection=products selector={"$query"=>{"shop_id"=>"511c7866fd896b1908000002"}, "$orderby"=>{:_id=>1}} flags=[:slave_ok] limit=-1 skip=0 batch_size=nil fields=nil (0.5682ms)
MOPED: 127.0.0.1:27017 QUERY database=bamboo_development collection=products selector={"$query"=>{"shop_id"=>"511c7866fd896b1908000002"}, "$orderby"=>{:_id=>1}} flags=[:slave_ok] limit=-1 skip=0 batch_size=nil fields=nil (1.1139ms)
MOPED: 127.0.0.1:27017 QUERY database=bamboo_development collection=products selector={"$query"=>{"shop_id"=>"511c7866fd896b1908000002"}, "$orderby"=>{:_id=>1}} flags=[:slave_ok] limit=0 skip=0 batch_size=nil fields=nil (0.4492ms)
MOPED: 127.0.0.1:27017 QUERY database=bamboo_development collection=products selector={"$query"=>{"shop_id"=>"511c7866fd896b1908000002"}, "$orderby"=>{:_id=>1}} flags=[:slave_ok] limit=0 skip=0 batch_size=nil fields=nil (0.4332ms)
当我这样做时:
@products = current_shop.products.search(params[:query])
render text: @products.map(&:name).join(",")
我只收到一个与@products 相关的请求:
MOPED: 127.0.0.1:27017 QUERY database=bamboo_development collection=products selector={"shop_id"=>"511c7866fd896b1908000002"} flags=[:slave_ok] limit=0 skip=0 batch_size=nil fields=nil (0.4611ms)
重复的查询看起来很奇怪,有人知道为什么会这样吗?它不调用子对象,只是与我所看到的有所不同的集合查询。