So I have a nested activerecord which contains an array of hashes. I am trying to get the country in an app I am making using a country code that is stored in one of the elements in the array.
the record is described:
user.rules.first.countries.first["country_code"]
user has_many rules,
rules contains a jsonb column called countries
countries is a jsonb array of hashes
at the moment I am iterating through all of them to find the record. e.g.
country_code_to_find = "US"
user.rules.each do |r|
r.countries.each do |c|
if c["country_code"] == "US"
# Do some stuff
end
end
end
Is there a way I can access that country with a single line using a .where() or scope or something like that? I am using rails 4, activerecord and postgres.