"145952912234658", "_type"=>"Page"}, {"_id"=>"465" /> "145952912234658", "_type"=>"Page"}, {"_id"=>"465" /> "145952912234658", "_type"=>"Page"}, {"_id"=>"465"/>

Rails mongoid query string in array of hashes

740 Views Asked by At

I have a Model that is called teams.

When I do Teams.account_ids it returns something like:

 [{"_id"=>"145952912234658", "_type"=>"Page"},
 {"_id"=>"465641870160985", "_type"=>"Account"}]

Lets say I want to get all Teams that have one specific account id regardless of its _type.

Something like:

Team.where(some_id.in => account_ids.map{|k| k["_id"))
1

There are 1 best solutions below

2
mu is too short On BEST ANSWER

You can use multi-keys to effectively ignore the array when searching and then use the standard "key inside a hash" notation to look at the _ids:

Teams.where('account_ids._id' => { :$in => array_of_ids })