我已经包含了给定的代码
@operator_selected = Operator.where(:id => selected_operator).map{|opr| opr.name}
我想按名称显示运算符排序。请指导我如何做到这一点。
答案 0 :(得分:2)
试试这个
@operator_selected = Operator.where(:id => selected_operator).order(:name).map{|opr| opr.name}
答案 1 :(得分:0)
只需使用此 -
@operator_selected = Operator.order(:name).where(id: selected_operator).map(&:name)
如果您始终希望按名称排序,为什么不按名称添加默认范围或只是按名称添加范围
喜欢
class Whatever < ActiveRecord::Base
default_scope order(:name)
end
并使用此:
@operator_selected = Operator.where(id: selected_operator).map(&:name)
OR
class Whatever < ActiveRecord::Base
scope :by_name, -> {order(:name)}
end
并使用此:
@operator_selected = Operator.by_name.where(id: selected_operator).map(&:name)