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