ないSQLの専門家が、私は(ソートなし)を取得すると、このデータと周り
User.create([
{ first_name: "Random", last_name: "Last" },
{ first_name: "Random", last_name: "Random" },
{ first_name: "First", last_name: "Random" },
{ first_name: "First", last_name: "Last" },
{ first_name: "Not First", last_name: "Last" },
{ first_name: "First", last_name: "Not Last" },
{ first_name: "Not First", last_name: "Not Last" }
])
を再生する:ここで
は私の現在のクエリです
User.where("first_name like ? or last_name like ?", "%First%", "%Last%").collect { |u| [u.first_name, u.last_name] }
# User Load (0.2ms) SELECT "users".* FROM "users" WHERE (first_name like '%First%' or last_name like '%Last%')
=> [["Random", "Last"], ["First", "Random"], ["First", "Last"], ["Not First", "Last"], ["First", "Not Last"], ["Not First", "Not Last"]]
と私は.order(User.send(:sanitize_sql, ["first_name like ? and last_name like ? DESC, first_name like ? OR last_name like ? DESC", "%First%", "%Last%", "%First%", "%Last%"])
に追加した場合、私は
0を取得します
User.where("first_name like ? or last_name like ?", "%First%", "%Last%").order(User.send(:sanitize_sql, ["first_name like ? and last_name like ? DESC, first_name like ? OR last_name like ? DESC", "%First%", "%Last%", "%First%", "%Last%"])).collect { |u| [u.first_name, u.last_name] }
# User Load (0.2ms) SELECT "users".* FROM "users" WHERE (first_name like '%First%' or last_name like '%Last%') ORDER BY first_name like '%First%' and last_name like '%Last%' DESC, first_name like '%First%' OR last_name like '%Last%' DESC
=> [["First", "Last"], ["Not First", "Last"], ["First", "Not Last"], ["Not First", "Not Last"], ["Random", "Last"], ["First", "Random"]]
これらのことの専門家がもっと多い人は、より良い方法を知っているかもしれませんが、これは完了したようです。