Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the resultant array from a Mongoid::Criteria without an "each" block

Our application uses ajax very heavily and as a result of this we have statements like var items = #{@items.to_json} in all our views. Now @items is being set in the controller as @items=Item.all. The problem is that @items is a Mongoid::Criteria and it doesn't have a .to_json method. So, it's throwing up an error while rendering the view. Is there an easy way to convert this criteria object into an array without using code like @items.collect {|i| i}

like image 942
Khaja Minhajuddin Avatar asked Dec 21 '10 11:12

Khaja Minhajuddin


1 Answers

Use the #entries method in criteria to do request:

@items = Item.all.entries 
like image 137
shingara Avatar answered Oct 11 '22 17:10

shingara