2012-04-18 63 views

回答

5

當然有,事實上,這也正是你說,除了你的方法調用關閉。

def index 
    @items = Item.order(:date) 
end 

你的索引視圖(可能index.html.erb)正在尋找一個@items陣列,對不對?那麼你可以格式化/命令/等數據,但你認爲合適,沒問題!

3

試試這個:

@items = Item.all 
@items.sort! { |a,b| a.date <=> b.date } 

有是Ruby的支持的另一種方法:

@items = Item.all.sort_by &:date