我有一个哈希数组。
SELECT persons.* FROM persons WHERE persons.id = 20 AND persons.name = 'John' OR persons.id = 30 AND persons.name = 'Doe'
我想获得符合特定哈希中所有条件的记录。 所以我想要执行的查询是
static double incrTrueBinaryErr = 62.5;
static double incrTrueBinaryMov = 31.25;
static double incr01001 = 360000;
static double incr_1101 = 125;
static double incr05008 = 3600000;
static double incr10004 = 1024000;
static double incr22000 = 1000;
static double incr22001 = 1000;
Date currDate;
while (true)
{
currDate = new Date();
if (currDate.getTime() == currDate.getTime() + incrTrueBinaryErr)
//do stuff
if (currDate.getTime() == currDate.getTime() + incrTrueBinaryMov)
//do stuff
if (currDate.getTime() == currDate.getTime() + incr01001)
//do stuff
if (currDate.getTime() == currDate.getTime() + incr_1101)
//do stuff
if (currDate.getTime() == currDate.getTime() + incr05008)
//do stuff
if (currDate.getTime() == currDate.getTime() + incr10004)
//do stuff
if (currDate.getTime() == currDate.getTime() + incr22000)
//do stuff
if (currDate.getTime() == currDate.getTime() + incr22001)
//do stuff
}
从哈希数组构造此查询的最佳方法是什么?
答案 0 :(得分:0)
尝试将所有条件独立映射到ActiveRecord模型,然后展平结果数组:
array_of_hash.map{ |where_clause| Person.where(where_clause) }.flatten
答案 1 :(得分:0)
我认为这没关系:
ids = array_of_hash.map { |h| h[:id] }
names = array_of_hash.map { |h| h[:name] }
Person.where(id: ids, name: names)
(虽然它不是超级通用的) 其他尝试:
people = Person.all
array_of_hash.each do |h|
people = people.where(h)
end
people # => will generate a long long query.