我有一个循环用于order_by for:created_at和:desc
<% for comment in post.comments.order_by([:created_at,:desc]) %> <% end %>
如何在每个e.j的block中执行order_by([:created_at,:desc])?
<% post.comments.each do |comment|%> <% end %>
编辑
对我来说工作正常的代码:
post.comments.order([:created_at,:desc])[0,5].each do |comment|
[0,5]将结果限制为间隔.
解决方法
订单方式是您要查找的:
<% post.comments.order("created_at desc").each do |comment|%> <% end %>