Reiterate over collection
<p>I have an unknown number of categories. </p>

<p>I want to pick one post from each category, and when there are no more categories I want to start from the beginning until I've reached a fixed number posts.</p>

<p>This is what I have, how could I rerun this iteration until I have my desired amount of posts?</p>

<pre><code>desired_amount = 40
categories.each_with_index do |category, index|
  post = category.posts.order(position: :asc)[index]
  # do something with the post
  return if desired_amount == (index + 1)
end
</code></pre>