我正在尝试按domains
排序rank
,这是metrics
属性的众多键之一。
这是一个对象的例子:
Domain.all.first.attributes
=> {
"id" => 1,
"name" => "example.com",
"status" => "active",
"country" => "Generic",
"languages" => [],
"ips" => [],
"created_at" => Sun, 25 Dec 2016 11:03:11 UTC +00:00,
"updated_at" => Mon, 06 Feb 2017 18:17:01 UTC +00:00,
"metrics" => {
"internal_code" => 14803,
"language" => "",
"rank" => 98.4011625387248
}
}
目前我只是按created_at
订购:
def index
@domains = filter(@domains.order(created_at: :asc))
如何按rank
订购域名?
答案 0 :(得分:1)
如果是JSON字段,您可以按以下方式订购。
span {
display: visible;
}
答案 1 :(得分:1)
问题是它会排序等级,但作为ex的字符串。 0, 1, 11, 15 2, 21, 3, 40, 5
您需要将其投放到float
或integer
,然后才能订购
Domain.order("(metrics ->> 'rank')::float")
#=> SELECT "domains".* FROM "domains" ORDER BY (metrics ->> 'rank')::float
如果需要,您可以在降序中订购
Domain.order("(metrics ->> 'rank')::float DESC")
#=> SELECT "domains".* FROM "domains" ORDER BY (metrics ->> 'rank')::float DESC
答案 2 :(得分:0)
尝试以下内容。
@domains = filter(@domains.order("metrics.rank"))