Reputation: 48490
Is it currently possible to search an array of BSON::OrderedHashes with Mongoid based objects? One of my attributes, let's call it foo
in the bar
class, looks something like the following:
[#<BSON::OrderedHash:0x7f0 {"id"=>"4bf58dd8d48988d118951735", "name"=>"Grocery or Supermarket", "pluralName"=>"Grocery or Supermarkets", "shortName"=>"Grocery Store", "icon"=>#<BSON::OrderedHash:0x7f2 {"prefix"=>"https://foursquare.com/img/categories/shops/food_grocery_", "sizes"=>[32, 44, 64, 88, 256], "name"=>".png"}>, "primary"=>true}>]
I want to go through all of my objects and find every one that contains Grocery in this order hash for the 'name' attribute.
Upvotes: 1
Views: 1737
Reputation: 434965
I think you should be able to do something like this:
Bar.where('foo.name' => /Grocery/)
MongoDB's multikeys support will take care of searching through the array's elements and 'foo.name'
will look for name
within foo
.
Upvotes: 6
Reputation: 10328
Haven't tested this, but I think something like this would probably work.
Bar.all.select {|b| b.foo.select{|f| f['name'] == 'Grocery'}.size > 0}
Upvotes: 0