I am making a model in ruby like the one I have shared below. The problem is that I need to reference the key-value pairs in locations for my filters. So how should I go about in doing that?
class User
include Mongoid::Document
store_in collection: "user"
field :name, type: String
field :number, type: String
field :locations #is an array of locations with key-value pairs
end
for example, the user object is like
db.user.findOne()
{
"name" : "ABC",
"number" : "1234567890",
"locations" : [
{
"state" : "X",
"district" : "AY",
},
{
"state" : "V",
"district" : "BZ",
}
],
}
Using filters I need to find all users who are in say state = "X". Mongo query will use locations.state but how to define a field that can help in querying such states. For querying in filters I am using Active Admin like below:
filter :locations, label: 'Locations', as: :select,
collection: {
'x': 'X',
'v': 'V',
}
So, I need a way to query states in locations rather than locations. Any help in how to approach this problem will be really helpful.
Let me know if any other details are needed.
Thanks in Advance!