Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我用
Post.order(:created_at)
在控制器中,帖子按创建时间排序,但它是从最旧的帖子到最新的帖子,这是我不想要的,我想更改顺序以首先显示最近的帖子,我该如何更改编码?
采用Post.order("created_at desc")
Post.order("created_at desc")
提示:使用 ascope以便此逻辑保留在您的模型上,而不是在您的控制器上。
scope
class Post < ActiveRecord::Base scope :recent_first, order("created_at desc") end
然后,在您的控制器上,您可以使用Post.recent_first.
Post.recent_first