【发布时间】:2016-07-25 18:53:29
【问题描述】:
我正在尝试按降序显示由median_salary 排序的作业列表。到目前为止,它似乎只考虑了median_salary的第一个数字。所以像 900 这样的东西列在 1000 之上,即使 1000 > 900 的值。
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。
标签: ruby-on-rails sorting