订购 while Rails has_many 并将字符串类型转换为十进制

Ordering while Rails has_many with typecast String to decimal

我已经 Size 模型与 value 作为一个字符串。 我想根据 value 属性通过将其转换为十进制来订购 sizes

has_many :sizes, -> {order 'value ASC'},这是按字母顺序排列的。

我试过了has_many :sizes, -> {order 'value::integer ASC'},没用。

谢谢

我找到了解决方案。我们可以 CAST 如下所示的字段。

has_many :sizes, -> { order 'CAST(value AS DECIMAL) ASC' }.