1
我想根據選擇從用戶表的用戶:排序在選擇條款,支持多列
user.created
user.sales_count
所以我想獲取所有用戶,有時創建日期排序,有時根據sales_count。我希望能夠在ASC或DESC命令之間切換。
所有的查詢需要有這樣的WHERE子句:
WHERE region = 123
如何建立我的活動記錄查詢,支持條件下,這些訂單?
我想根據選擇從用戶表的用戶:排序在選擇條款,支持多列
user.created
user.sales_count
所以我想獲取所有用戶,有時創建日期排序,有時根據sales_count。我希望能夠在ASC或DESC命令之間切換。
所有的查詢需要有這樣的WHERE子句:
WHERE region = 123
如何建立我的活動記錄查詢,支持條件下,這些訂單?
def get_users(options={})
options[:order_col] ||= "created"
options[:order_type] ||= ""
User.where(:region=>123).order("#{options[:order_col]} #{options[:order_type]}")
end
options[:order_col] ||=
實際上是說:
options[:order_col] = options[:order_col] || ""
這在英國是說設置options[:order_col]
到options[:order_col]
如果一套,其他明智""
。我們可以將order_type設置爲""
,因爲默認情況下SQL將結果爲ASC。
例子:
get_users #=> return ordered by created ASC
get_users(:order_col => "sales_count") #=> return order by sales count ASC
get_users(:order_col => "sales_count", :order_type => "DESC") #=> sales_count, DESC
# etc
不這樣離開你打開到SQL注入,但? – Blankman 2011-07-18 23:52:23