我有一个名为“候选”的模型,“状态”列为字符串。我想按状态从“新”到“待定”对记录进行排序。我的代码在Sqlite3中可用,但在PostgreSQL中不可用。
这是我的模特
class Candidate < ApplicationRecord
belongs_to :collaborator, optional: true
has_many :curriculum_vitaes, dependent: :destroy
def self.order_by_status
order_by = ["CASE"]
STATUS.each_with_index do |status, index|
order_by << "WHEN status='#{status}' THEN #{index}"
end
order_by << "END ASC, name ASC"
order(order_by.join(" "))
end
STATUS = [
"New",
"Received CV",
"Checking CV",
"Waiting Interview",
"Waiting Result",
"Passed",
"Failed",
"Pending"
]
在控制器中,当我通过
@candidates = Candidate.order_by_status
我总是会出错:
PG::InvalidColumnReference: ERROR: for SELECT DISTINCT, ORDER BY expressions must appear in select list
你能帮我吗?