我有这个循环:
<% for post in posts.order_by([:created_at, :desc]).limit(6) %>
post.name
<% end %>
这很好用,但我现在想要获得每个帖子的名称,随机顺序和限制为6。
我想如何使用和每个块一样:
posts.each do |post|
post.name
end
答案 0 :(得分:2)
posts.limit(6).shuffle.each do |post|
post.name
end
答案 1 :(得分:1)
我不完全确定你在问什么,但听起来像你可以这样做:
<% posts.limit(6).shuffle.each do |post| %>
<%= post.name %>
<br>
<% end %>
See the docs for the Array class's shuffle method了解有关随机化用途的详细信息。