從在這裏的其他問題和答案創建-的,我可以看到,創建「下一步」和「上一個」鏈接,你可以做到這一點排序方式與下一個和以前的帖子鏈接
def previous_post
self.class.first(:conditions => ["title < ?", title], :order => "title desc")
end
def next_post
self.class.first(:conditions => ["title > ?", title], :order => "title asc")
end
將下令如果它們是獨一無二的,我可以用標題來表示。我不太瞭解條件語法,並想知道是否有人可以解釋這一點。
如果我想按照created_at日期排序,我可以將順序更改爲created_at desc和created_at asc,但那麼我的條件是什麼?
任何幫助表示讚賞
感謝
這只是你沒有在鐵軌2.他們是活躍記錄查詢方式使用它是因爲它們按標題排序。您的條件將會是'[「created_at <?」,created_at]'爲previous和'[「created_at>?」,created_at]'爲下一篇文章 –
會有一種更有效的方式在Rails 4中做到這一點嗎?或者那個語法正確?還有什麼是這樣說[「created_at <?」,created_at],只是這樣我可以在我的腦海中得到它 – Richlewis