2013-02-15 226 views
0

我有一個使用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 

當我打它,日誌顯示4個有關@products請求:

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) 

的重複查詢看起來很奇怪,沒有人知道爲什麼出現這種情況?它不會調用子對象,只是有點不同的集合查詢,從我看到的。

+0

嘗試@products = current_shop.products.to_a – Aymeric 2013-02-22 23:52:42

回答

0

很多人得到同樣的問題。最有可能是因爲拉皮配置問題。根據上面的Aymeric推薦,只需執行以下操作: @products = current_shop.products.to_a