Reputation: 888
When performing a block like:
<% @user.favoured_user.each do |user| %>
<li><%= user.name %></li>
<% end %>
With the favoured_user method returning a limit of 5 users, how would I manipulate the block so that even when there are only 3 users available, I could still return 5 li elements?
I'm guessing a helper would come in to play, and maybe the 'first, second, third, etc.' array methods, but I can't think how to write it.
Any help?
Upvotes: 1
Views: 109
Reputation: 44962
You can use in_groups_of
Like:
<% @user.favoured_user.in_groups_of(5).each do |favored_user| %>
<% favored_user.each do |user| %>
<li><%= user.try(:name) %></li>
<% end %>
<% end %>
The first 3 users will come through, and the last two entries will be nil
Upvotes: 1
Reputation: 15492
You can try this,
<% 5.times do |i| %> <li> <%= @user.favoured_user[i].try(:name) %> </li> <% end %>
Upvotes: 2