0

I have a simple app that uses rabl and mongoid 3.1.0, with this action:

# index
@products = current_shop.products

The rabl code looks like this:

# index.json.rabl
collection @products
extends 'api/products/show'

# show.json.rabl
object @product
attributes :id

When I hit it, the logs show 4 requests related to @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)

When I do this:

@products = current_shop.products.search(params[:query])
render text: @products.map(&:name).join(",")

I get only one request related to @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)

The duplicated queries look strange, does anyone know why that happens? It doesn't call child objects, just somewhat different collection queries, from what I see.

Greg Funtusov
  • 1,377
  • 15
  • 18

1 Answers1

0

Lots of people getting same problem. Most likely because of rabl configuration problem. As recommended by Aymeric above, just do: @products = current_shop.products.to_a

daniel
  • 1,205
  • 1
  • 10
  • 15