Rails: How to order by descending value in controller -
i trying show list of jobs ordered median_salary
descending order. far, seems take account first number of median_salary
. 900 listed above 1000, though value of 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 %>
returns:
#<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">]>
it listing 95928.48 higher 170507.69. missing condition? i've looked @ best way implement sort asc or desc in rails , seemed suggest way writing sort.
any insight or advice help. thanks!
it's because median_salary
database field string , it's sorted string. need cast integer in order clause, or create migration, change field datatype.
difference between strings being sorting , floats being sorted:
irb(main):001:0> ["95928.48", "170507.69"].sort => ["170507.69", "95928.48"] irb(main):002:0> [95928.48, 170507.69].sort => [95928.48, 170507.69]
in postgres order clause should looks this:
@nyc_highest = nyc_highest.order("cast(median_salary float) desc")
Comments
Post a Comment