Ruby on rails 如何按日期和时间对帖子进行排序?

Ruby on rails 如何按日期和时间对帖子进行排序?,ruby-on-rails,ruby,rack,Ruby On Rails,Ruby,Rack,我正在运行一个toto-powered博客,我正在尝试按日期对文章进行正确排序(如果我一天发布一次以上,文章将按当天的字母顺序排序)。现在,在我的config.ru中,我有了日期的基本设置,包括#set:date,lambda{now | now.strftime(“%d/%m/%Y”)}和时间设置#set:time,lambda{now | now.strftime(“at%H:%I%p”)} 在my layout.rhtml中,文章的排序如下:在文章中添加一个名为time的字段: titl

我正在运行一个toto-powered博客,我正在尝试按日期对文章进行正确排序(如果我一天发布一次以上,文章将按当天的字母顺序排序)。现在,在我的config.ru中,我有了日期的基本设置,包括
#set:date,lambda{now | now.strftime(“%d/%m/%Y”)}
和时间设置
#set:time,lambda{now | now.strftime(“at%H:%I%p”)}


在my layout.rhtml中,文章的排序如下:
在文章中添加一个名为time的字段:

title: The Wonderful Wizard of Oz
author: Lyman Frank Baum
date: 1900/05/17
time: 12:30:00 PST

Dorothy lived in the midst of the great Kansas prairies, with Uncle Henry,
who was a farmer, and Aunt Em, who was the farmer's wife.
在服务器块之前对项目类进行Monkey修补:

require 'time'

class Article  
  def timestamp
    self[:timestamp] ||= Time.parse("#{self[:date].strftime("%Y-%m-%d")} #{self[:time]}")
  end
end  

toto = Toto::Server.new do
现在,在布局中,您可以使用
时间戳
方法进行排序:

<% articles.select {|a| a.timestamp <= Time.now}[0..4].each do |article| %>