order_by in block each rails 3.1

时间:2011-12-30 13:13:38

标签: ruby-on-rails ruby-on-rails-3 sql-order-by each

我有一个循环for order_by for:created_at和:desc

<% for comment in post.comments.order_by([:created_at, :desc]) %>
<% end %>

我怎样才能在每个块中执行order_by([:created_at,:desc]),e.j:

<% post.comments.each do |comment|%>
<% end %>

被修改

适用于我的代码:

post.comments.order([:created_at, :desc])[0,5].each do |comment|

使用[0,5]将结果限制为间隔。

1 个答案:

答案 0 :(得分:30)

订购方法正是您所需要的:

<% post.comments.order("created_at desc").each do |comment|%>
<% end %>