2013-11-24 33 views
0

我在我的url中有以下參數。rails 4條件之間的多個範圍

考慮url../price=0-1000_2000-5001

我已經轉換了以上價格PARAMS作爲

pri = [["0", "1000"], ["2000", "5001"]] #This is not limited to only two 

現在我怎麼可以查詢在軌以下

select * from product where price betweeen 0 and 1000 and price between 2000 and 5001 and so on 

我發現下面的查詢中軌

Product.where(price: pri[0].first..pri[0].last) 

但是我怎麼能找到多個範圍。

編輯-1

@posts = Post.where(價格:PRICE_RANGE)#此工作現在細

[1001..5000,10000..0]

這裏10000..0標明價格greater than 10000

但我查詢作爲

執行0

SELECT * FROM職位,其中1001和5000的價格之間的價格10000和0

但查詢應該像之間的這種

SELECT * FROM職位,其中1001和5000的價格> 10000

之間價格
checkbox1 = value 0-1000 
checkbox2 = value 1001-5000 
checkbox3 = value 5001-10000 
checkbox4 = value 10001-15000 
checkbox5 = value 15001-0(This indicates price > 15001) 

回答

0

這是一個使用Range對象而不是手動表示範圍的解決方案。和ARel而不是硬編碼的SQL。通常使用ARel over SQL字符串是很好的做法,以便ARel可以處理格式化SQL的數據庫適配器,以適應您現在和將來可能使用的任何數據庫適配器。

price_ranges = [0..1000, 2000..5001] 

# Set up the base relation 
products = Product.where(price: price_ranges.pop) 

# iterate on any remaining prices 
products = price_ranges.inject(products) do |rel, price_range| 
    # Use ARel's `or` method to chain on to the relation 
    rel.or(Product.arel_table[:price].in(price_range)) 
end if price_ranges.any? 

如果需要,你可以先把你的價格數組轉換爲範圍是這樣的:

price_ranges = prices.map { |ar| Range.new(Integer(ar.first), Integer(ar.second)) } 

更新:

如果你想繼續使用範圍,但允許一個值可以大於某個數字,您可以使用Float::INFINITY來完成此操作:

price_ranges = [1001..5000, 10000..Float::INFINITY] 

# For example: 
Product.where(price: 10000..Float::INFINITY).to_sql 
    # => SELECT "products".* FROM "products" WHERE ("products"."id" >= 10000) 

當然,這可能意味着當構建範圍數組時,您需要有條件地將第二個值設置爲Float::INFINITY

+0

您能否清楚一點嗎?檢查我的編輯 – overflow

+0

@Seting,更新了我的答案,以幫助您瞭解更多條件。 – pdobb

+0

那好,但它不是第二個。正如我之前所說,這不僅限於兩個,它是動態的。你可以檢查我更新的問題 – overflow

0
Product.where("(price >= ? AND price <= ?) OR (price >= ? AND price <= ?)", pri[0].first, pri[0].last, pri[1].first, pri[1].last) 

像這樣的東西應該可以工作,但我沒有測試這個代碼。

+0

'[[「0」,「1000」],[「2000」,「5001」]]這不限於兩個。它可能有更多,因爲它是動態的 – overflow

+1

使用循環動態生成搜索字符串。 –

0

而不是使用和之間的兩個使用範圍或本必須返回一些產品

Product.where("price between ? and ? or price between ? and ?", pri[0].first, pri[0].last, pri[1].first, pri[1].last) 

編輯動態數組lenght

arr = [["0", "100"], ["100", "200"]] 
query_string = "" 
arr.each {|a| query_string << (arr.last == a ? " price between #{a.first} and #{a.last}"  : "price between #{a.first} and #{a.last} or") } 
Product.where(query_string) 

找到預期的記錄。

+0

[[「0」,「1000」],[「2000」,「5001」]]不限於兩個。它可能有更多的,因爲它是動態的 – overflow

+0

Ok @Seting checkout編輯 – nishanthan

+0

您編輯的位置我無法看到 – overflow

0

有一個更簡單的解決方案。這就是你需要:

price_ranges = [0..1000, 2000..5001] 
products = Product.where(price: price_ranges) 

這裏的控制檯輸出,具有類似的查詢,顯示系統產生的SQL:

>> ranges 
=> [-Infinity..1000000, 10000000..19999999] 
>> Company.where(total_funding_amount: ranges) 
Company Load (0.8ms) SELECT "companies".* FROM "companies" WHERE ((("companies"."total_funding_amount" <= 1000000 OR "companies"."total_funding_amount" BETWEEN 10000000 AND 19999999) OR 1=0)) 

你甚至可以使用的範圍和整數的組合。如果您好奇,請參閱this Rails issue from 2011中的討論。