[英]What's the simplest way to include the COUNT of associations in a SQL query?
[英]What is the simplest way to order results from a SQL query on part of a string field in Rails?
考慮一個帶有以下形式的文本字段的表:
"foo,bar"
編輯:“bar”是逗號后面的第一個字符串。 只有一個逗號。
什么是最簡單的bar
訂購方式? 它可以是SQL(PostgreSQL)或Ruby。
MyTable.all.sort_by { |r| r.my_text.match(/,(.*)$/)[1] }
您可以在數據庫中執行此操作:
M.order("substring(c from ',.*$')")
其中M
是你的模型, c
是有問題的列。 substring
調用在子字符串中保留逗號但不會改變排序,並且在排序期間比較逗號可能比嘗試刪除它們更快。 此外,PostgreSQL的substring
形式應該在8.2中工作,因此它應該與Heroku共享數據庫一起使用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.