我正在尝试按降序显示median_salary
排序的作业列表。到目前为止,它似乎只考虑了median_salary
的第一个数字。因此,在1000以上列出900之类的东西,即使1000的值> 1。 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,这似乎表明我目前正在编写这种方式。
答案 0 :(得分:2)
这是因为您的median_salary
数据库字段是字符串,并且它按字符串排序。您需要在顺序子句中将其强制转换为整数,或者创建一个迁移,这将更改字段数据类型。
正在排序的字符串和要排序的浮点数之间的区别:
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]
在postgres中,您的订单子句应如下所示:
@nyc_highest = nyc_highest.order("CAST(median_salary as FLOAT) DESC")
答案 1 :(得分:1)
正如@teksisto所说,你应该改变浮动的var queueName = 'my_token_bucket';
rabbitChannel.assertQueue(queueName, {durable: true, messageTtl: 1000, maxLength: bucket.ratePerSecond});
writeToken();
function writeToken() {
rabbitChannel.sendToQueue(queueName, new Buffer(new Date().toISOString()), {persistent: true});
setTimeout(writeToken, 1000 / bucket.ratePerSecond);
}
或接受小数的某种类型。另外,我建议在你的模型上创建一个范围,比如
median_salary
在scope :nyc_highest, -> { where("city = ?", "NYC").order("median_salary DESC") }
模型上。然后,您只需在您喜欢的应用程序的任何位置调用Highestpaidjob
。
更改Highestpaidjob.nyc_highest
数据类型:
median_salary
然后编辑您的迁移文件:
rails g migration ChangeMedianSalaryType