一个ActiveRecord查询我有以下查询该工程只是罚款:构建基于条件
temp = Apartment
.where(rooms: roomsArray)
.where(area: areasArray)
.where(price_interval: price_intervalsArray)
.group(:rooms)
.count
现在,我想如果某些条件得到满足为他们每个人只适用。哪里查询。所以如果roomsArray是空的,我们跳过整个.where(rooms: roomsArray)
-查询。而areasArray和price_intervalsArray也是一样的。
我怎样才能建立与条件查询?
理想的情况下它会是这个样子:
temp = Apartment
unless roomsArray.empty?
.where(rooms: key)
end
unless areasArray.empty?
.where(area: areasArray)
end
unless price_intervalsArray.empty?
.where(price_interval: price_intervalsArray)
end
.group(:rooms)
.count