我有一个列表,我必须按出生日期排序,只按月份排序,不考虑年份。从一月到十二月。我如何在 ruby on rails 中做到这一点?
同样,我需要能够在每个人的编辑屏幕上看到以岁为单位的年龄,而不是显示 DD / MM / YY。
答案 0 :(得分:0)
如果您使用的是 Postgres....
您可以使用 extract method 创建一个可排序的字段:
@people = Person.select('extract("MONTH" from dob) as birth_month').order(birth_month: :asc)
其中 'Person' 是假定的表名,'dob' 是假定的列。
这是未经测试的,您可能需要在其中转换 ::date 或其他内容。不过,请查看 extract method。
答案 1 :(得分:-1)
dob = "01-01-2020"
def age(dob)
(Time.now.to_s(:number).to_i - dob.to_time.to_s(:number).to_i)/10e9.to_i
end