一個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