我试图通过降序排列显示median_salary
订购的作业列表。到目前为止,它似乎只考虑了第一批median_salary
。因此,像900 1000上面列出,即使1000的值> 900Rails:如何通过控制器中的降序进行订购
homes_controller.rb:
def index
nyc_highest = Highestpaidjob.where("city = ?", "NYC")
@nyc_highest = nyc_highest.order("median_salary DESC")
end
index.html.erb:
<%= @nyc_highest.inspect %>
回报:
#<ActiveRecord::Relation [#<Highestpaidjob id: 11, job: "Architect, City Planner", median_salary: "95928.48", count: 237, margin_of_error: "", city: "NYC", state: "New York", created_at: "2016-07-25 18:17:17", updated_at: "2016-07-25 18:17:17">, #<Highestpaidjob id: 7, job: "Medical", median_salary: "170507.69", count: 128, margin_of_error: "", city: "NYC", state: "New York", created_at: "2016-07-25 18:09:30", updated_at: "2016-07-25 18:09:30">]>
它列出了95928.48高于170507.69。我是否缺少一个条件?我看过Best way to implement sort asc or desc in rails,它似乎暗示了我目前正在写这种东西的方式。
任何见解或建议将有所帮助。谢谢!
您median_salary字段是一个字符串,这就是为什么你有这个问题。您需要通过迁移或'to_i'方法将您的字符串类型更改为int。 –