ruby on rails - ActiveRecord query from an array of hash -
i have array of hash.
eg) array_of_hash = [ { id: 20, name: 'john' }, { id: 30, name: 'doe'} ]
i records match criteria in particular hash. query want executed is
select persons.* persons persons.id = 20 , persons.name = 'john' or persons.id = 30 , persons.name = 'doe'
what best way construct query array of hash?
i think okay:
ids = array_of_hash.map { |h| h[:id] } names = array_of_hash.map { |h| h[:name] } person.where(id: ids, name: names)
(although it's not super generic) other attempt:
people = person.all array_of_hash.each |h| people = people.where(h) end people # => generate long long query.
Comments
Post a Comment